String.prototype.replaceAll = function( searchStr, replaceStr ) 
{ 
	var temp = this; 
	while( temp.indexOf( searchStr ) != -1 ) temp = temp.replace( searchStr, replaceStr ); 
	return temp; 
} 

function trim(s){
	var zszs="" ;
	if (typeof(s) == "undefined") {zszs=""; }else{ 
	if (s == null) { zszs="";} else { zszs= s.replace( /^\s*/, "" ).replace( /\s*$/, "" ); }
	}
	return zszs;
} 

function checkEMail (data)
{
	data = data.replace ( " ", "" );

	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if (filter.test(data)) {
		return true;
	} else {
		return false;
	}
}

function ctr_style (obj) {
	var balid_color = "#E2F5FC";
	obj.value = '';
	obj.focus();
	obj.style.background = balid_color;
	obj.style.borderBottom="1px solid white";
	obj.style.borderLeft="1px solid black";
	obj.style.borderTop="1px solid black";
	obj.style.borderright="1px solid white";
}

function signup_act(frmObj)
{
	var uname = frmObj.uname;
	var email = frmObj.email;
	var password = frmObj.password;

	var tmp = trim(uname.value);
	uname.value = tmp;
	if(tmp == "")
	{
		ctr_style(uname);
		uname.focus();
		return false;
	}

	tmp = trim(email.value);
	email.value = tmp;
	if(tmp == "")
	{
		ctr_style(email);
		email.focus();
		return false;
	}else{
		var ret = checkEMail (email.value);
		if ( !ret )
		{
			ctr_style(email);
			email.focus();
			alert ('Invalid Email');
			return false;
		}
	}

	tmp = trim(password.value);
	password.value = tmp;
	if(tmp == "")
	{
		ctr_style(password);
		password.focus();
		return false;
	}else if (password.value.length < 6 )
	{
		alert ('more than 6 characters');
		ctr_style(password);
		password.focus();
		return false;
	}

	postForm(frmObj, _signup_process);
	return false;
}


// ajax post method //
function newXMLHTTP() {
	if(window.XMLHttpRequest) {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch(e) {
			alert("XMLHTTP Fail.");
			return false;
		}
	} else if(window.ActiveXObject) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				alert("XMLHTTPFail.");
				return false;
			}
		}
	} else {
		alert("XMLHTTP Fail.");
		return false;

	}
	return xmlhttp;
}

function postForm(obj, callback) {
	var xmlhttp = newXMLHTTP();
	if(!xmlhttp) return false;

	var child = obj.elements;
	var data = new Array();
	for(i = 0; i < child.length;i++) {

		if(child[i].tagName != "INPUT" && child[i].tagName != "TEXTAREA" && child[i].tagName != "SELECT") continue;

		if(child[i].type == "submit" || child[i].type == "button" || child[i].type == "reset") continue;

		if((child[i].type == "radio" || child[i].type == "checkbox") && !child[i].checked) continue;

		if(child[i].getAttributeNode("required") && !child[i].value) {
			child[i].style.backgroundColor = "#FF9";
			child[i].focus();
			alert("invalid");
			return false;
		}
	   	data.push ( child[i].name+"="+child[i].value);
	}

	senddata = data.join("&");

	xmlhttp.open("POST", obj.action,true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlhttp.send(senddata);

	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200) {
				callback(xmlhttp);
			} else {
				alert(xmlhttp.status+" "+xmlhttp.statusText);
			}
		}
	}
}

function _signup_process(xmlhttp)
{
	var msg = xmlhttp.responseText;

	if(msg == "fail")
	{
		alert("Email exist already.");
	}else{
		alert("Frushi.com send you email for certification.");
		window.open("/index.php?menu=signin","_self");
	}
}

function new_password(frm)
{
	var new_pwd = frm.new_pwd;
	var newf_pwd = frm.newf_pwd;

	var tmp = new_pwd.value;
	if(tmp == "")
	{
		ctr_style(new_pwd);
		new_pwd.focus();
		return false;
	}

	tmp = newf_pwd.value;
	if(tmp == "")
	{
		ctr_style(newf_pwd);
		newf_pwd.focus();
		return false;
	}else if(new_pwd.value != newf_pwd.value)
	{
		alert("Fail password match.");
		new_pwd.value = "";
		newf_pwd.value = "";
		new_pwd.focus();
		return false;
	}
}

function sign_proc(frm)
{
	var email = frm.Email;
	var password = frm.Passwd;

	var tmp = trim(email.value);
	email.value = tmp;
	if(tmp == "")
	{
		ctr_style(email);
		email.focus();
		return false;
	}else{
		var ret = checkEMail (email.value);
		if ( !ret )
		{
			ctr_style(email);
			email.focus();
			alert ('Invalid Email');
			return false;
		}
	}

	tmp = password.value;
	if(tmp == "")
	{
		password.focus();
		return false;
	}
}

function require_cert()
{
	alert("Please check your email now and verify your new account by following the link inside this email message.");
}

function open_window(name, url, left, top, width, height, toolbar, menubar, statusbar, scrollbar, resizable)
{
	var toolbar_str = toolbar ? 'yes' : 'no';
	var menubar_str = menubar ? 'yes' : 'no';
	var statusbar_str = statusbar ? 'yes' : 'no';
	var scrollbar_str = scrollbar ? 'yes' : 'no';
	var resizable_str = resizable ? 'yes' : 'no';

	var w = window.open(url, name, 'left='+left+',top='+top+',width='+width+',height='+height+',toolbar='+toolbar_str+',menubar='+menubar_str+',statusbar='+statusbar_str+',scrollbars='+scrollbar_str+',resizable='+resizable_str);
	w.focus();
}
