var blueColor="#3399FF";
var redColor="#ff0000";
var checking = new Array(5);

checking[0] = 0;
checking[1] = 0;
checking[2] = 0;
checking[3] = 0;
checking[4] = 0;

function showLayer(){

	var obj = new Array(
				document.getElementById("loginPopupSignupForm_loginName"),
				document.getElementById("loginPopupSignupForm_pwd"),
				document.getElementById("loginPopupSignupForm_retype_password"),
				document.getElementById("loginPopupSignupForm_lastname"),
				document.getElementById("loginPopupSignupForm_firstname"),
				document.getElementById("loginPopupSignupForm_email"),
				document.getElementById("loginPopupSignupForm_mobileNum"),
				document.getElementById("loginPopupSignupForm_country"));

	layerElement = document.getElementById("overlay");
	
	for(var i = 0;i < obj.length;++i){
		obj[i].value = "";
	}
	
	document.getElementById("loginerror").innerHTML = "&nbsp;";
	document.getElementById("retypepassworderror").innerHTML = "&nbsp;";
	document.getElementById("emailerror").innerHTML = "&nbsp;"

	if(layerElement != null){
		layerElement.style.display = 'block';
	}
	
	loginElement = document.getElementById("logindiv");
	
    var IpopTop = (document.body.clientHeight - loginElement.style.height.replace("px",""))/2;
    var IpopLeft = (document.body.clientWidth - loginElement.style.width.replace("px",""))/2;

	loginElement.style.left = IpopLeft;
	loginElement.style.top = IpopTop;

	if(loginElement != null){
		loginElement.style.display = 'block';
	}

	document.getElementById("loginName").focus();
}

function hideLayer(){

	loginElement = document.getElementById("logindiv");
	
	if(loginElement != null){
		loginElement.style.display = 'none';
	}

	layerElement = document.getElementById("overlay");

	if(layerElement != null){
		layerElement.style.display = 'none';
	}
}

function isValidEmail(str){
	var val=true;
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	
	if (str.indexOf(at)==-1){
	   val = false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		val = false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		val = false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		 val = false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		 val = false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		 val = false
	 }
	
	 if (str.indexOf(" ")!=-1){
		 val = false
	 }
	 
	 return val;
}

function emailChk(str) {

	if(str.length == 0){
		checking[2] = 1;
		return true;
	}
	
	checking[2] = 0;
	val = isValidEmail(str);
	var errorDiv = document.getElementById("emailerror");

	 if(!val){
		 checking[2] = 0;
		 errorDiv.style.color = redColor;
		 errorDiv.innerHTML = badEmail;
	 }
	 else{
		 checking[2] = 1;
		 errorDiv.style.color = blueColor;
		 errorDiv.innerHTML = validEmail;
	 }

	 checkAllowCreate();
     return val;
}

function optionEmailCheck(str){
	var errorDiv = document.getElementById("emailerror");
	if(str.trim().length == 0){

		checking[2] = 1;
		errorDiv.innerHTML = "";
		checkAllowCreate();		
		return true;
	}
	else{
		checkAllowCreate()
		return emailChk(str);
	}
}

function agreeTC(chk){

	if(chk.checked)
		checking[3] = 1;
	else
		checking[3] = 0;
	
	checkAllowCreate();
}

function ageDeclare(chk){
	if(chk.checked)
		checking[4] = 1;
	else
		checking[4] = 0;
	
	checkAllowCreate();
}

function pwdChk(comparingObj, comparedObjName){
	checking[1] = 0;
	
	var comparedObj = document.getElementById("loginPopupSignupForm_"+comparedObjName);
	var errorDiv = document.getElementById("retypepassworderror");
	var val=true;

	errorDiv.style.color=blueColor;
	
	if(comparingObj.value != comparedObj.value){
		errorDiv.innerHTML = diffPwd;
		val=false;
	}

	else if(comparingObj.value.length < 3){
		errorDiv.innerHTML = badPwd;
		val=false;
	}
	
	if(!val){		
		checking[1] = 0;
		errorDiv.style.color=redColor;
	}
	else{
		checking[1] = 1;
		errorDiv.innerHTML = validPwd;
		errorDiv.style.color = blueColor;
	}

	checkAllowCreate();
	return val;
}

function checkName(obj){
	alert(obj);
}

function checkLoginAvailability(login){
	checking[0] = 0;
	if(login.length >= 2){
	
	    var postData = 'loginName='+login;
	    var callback = {
	               timeout: 5000,
	               success: function (o){
	    					checkRequestLogout(o.responseText);
	                       document.getElementById('loginerror').innerHTML = o.responseText;

	                       if(document.getElementById("validTag") != null){
	                    	   checking[0] = 1;
	                    	   document.getElementById('loginerror').style.color = "#3399FF";
	                       }
	                       else{
	                    	   checking[0] = 0;
	                    	   document.getElementById('loginerror').style.color = "#ff0000";
	                       }
	                       
	                       checkAllowCreate();
	                   },
	               failure: function(o){
	                	   document.getElementById('loginerror').innerHTML = "Error Occured"
	                	   document.getElementById('loginerror').style.color = "#ff0000";
	                	   checking[0] = 0;
	                	   checkAllowCreate();
	                   }
	    }
	
	    YAHOO.util.Connect.asyncRequest('POST', "checkLogin.do", callback, postData);
	}
	else{
		alert("Please input login name");
	}
 }

function checkAllowCreate(){
	var button = document.getElementById("createmem");

	if(checking[0] == 1 &&
	   checking[1] == 1 &&
	   checking[2] == 1 &&
	   checking[3] == 1 &&
	   checking[4] == 1){
		button.disabled = false;
	}
	else{
		button.disabled = true;
	}
}

function memberRegistration(){

	var login = document.getElementById("loginPopupSignupForm_loginName");
	var pwd = document.getElementById("loginPopupSignupForm_pwd");
	var ln = document.getElementById("loginPopupSignupForm_lastname");
	var fn = document.getElementById("loginPopupSignupForm_firstname");
	var email = document.getElementById("loginPopupSignupForm_email");
	var mobile = document.getElementById("loginPopupSignupForm_mobileNum");
	var country = document.getElementById("loginPopupSignupForm_country");

	createMem(login.value, pwd.value, ln.value, fn.value, 
			email.value, mobile.value, country.value);
}

function createMem(login, pwd, last, first, email, mobile, country){
    var postData = 'loginName='+login+'&pwd='+pwd+'&lastName='+last+'&firstName='+first+'&email='+email+'&mobileNum='+mobile+'&country='+country;
    var callback = {
               timeout: 5000,
               success: function (o){
    					checkRequestLogout(o.responseText);
    				   if(document.getElementById("validTag") != null){
    					   alert(regsuccess);
    					   hideLayer();
    				   }
    				   else{
    					   alert(regfail);
    				   }
                   },
               failure: function(o){
                	   alert(regerror);
                   }
    }

    YAHOO.util.Connect.asyncRequest('POST', "register.do", callback, postData);
}
