//====================================================================================================
//	File Name		:	registration.js
//	# File Version: v 1.0
//	# Created By: Maulik Chandarana
//	# Created On: 11 June 2007
//	# Last Modified By:
//	# Last modified On:
//  # Purpose : This is file carries validation and other related function for Registration page.
//----------------------------------------------------------------------------------------------------

//====================================================================================================
//	Function Name	:check_Username()
//	Created By: Srikanta Biswas
//	Created On: 7 Sept 2007
//	Last Modified By:
//	Last modified On:
//  Purpose : Validates form variables 
//  Parameters: frm : Form name for which variables need to validate.
//----------------------------------------------------------------------------------------------------
var xmlHttp;

function check_Username(frm)
{
	var regex = /^[a-zA-Z0-9-_.]+$/ ;
	if(!regex.test(frm.txtUsername.value)) 
	{
			alert("Username can contain only letters, numbers, periods (.), hyphens (-), and underscores (_). Special characters or accented letters are not allowed. Please type a different UserName");
			frm.txtUsername.focus();
			return false;
	}
	//Check For Minimum Character.
	
	if(Trim(frm.txtUsername.value).length < 4)
	{
		alert("Username Should Not Be Less Than 4 Characters");
		frm.txtUsername.focus();
		return false;
	}
	
	if(!fnCheckForBlankSpace(frm.txtUsername, 'Please Remove Any Blank Space From Username'))
	{
		return false;
	}
	document.getElementById("chk_user").value = "Checking....";	
	name = frm.txtUsername.value;
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null)
	{
		alert ("Your Browser Does Not Support AJAX!");
		return;
	} 
	var newurl="check.php";
	newurl=newurl+"?valusername="+name;
	xmlHttp.onreadystatechange=newstateChanged;
	xmlHttp.open("GET",newurl,true);
	xmlHttp.send(null);

}
function newstateChanged() 
{ 
	if (xmlHttp.readyState==4)
	{
        if(xmlHttp.responseText == "Username Is Available")
            document.getElementById("vusername").innerHTML="<span class='Bluematter'>"+xmlHttp.responseText+"</span>";
        else
            document.getElementById("vusername").innerHTML="<span class='Redmatter'>"+xmlHttp.responseText+"</span>";	
			document.getElementById("chk_user").value = "Check Availability";
	}
	
}


//====================================================================================================
//	Function Name	:check_Email()
//	Created By: Srikanta Biswas
//	Created On: 7 Sept 2007
//	Last Modified By:
//	Last modified On:
//  Purpose : Validates form variables 
//  Parameters: frm : Form name for which variables need to validate.
//----------------------------------------------------------------------------------------------------
 
function check_Email(frm)
{
	email = frm.txtEmail.value;	
	/*********** Email validation at check availability Added by amit*********/
	email_obj = frm.txtEmail;
	/*var regex = /^[\w]+(\.[\w]+)*@([\w]+\.)+[a-zA-Z]{2,7}$/ ;
	var mat = regex.test(email_obj.value);
	if(!mat)
	{
			alert('Please Enter Valid Email Address.');
			return false;
	}*/
	if(!IsEmpty(email_obj, 'Please Enter The Email Address.'))
	{
		return false;
	}
	if(!IsEmail(email_obj,'Please Enter Valid Email Address.'))
	{
		return false;
	}
	document.getElementById("chk_mail").value = "Checking....";
	/*********************************************************/	
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null)
	{
		alert ("Your Browser Does Not Support AJAX!");
		return;
	} 
	var newurl="check.php";
	newurl=newurl+"?valemail="+email;
	xmlHttp.onreadystatechange=newstateChanged1;
	xmlHttp.open("GET",newurl,true);
	xmlHttp.send(null);

}
function newstateChanged1() 
{ 
	if (xmlHttp.readyState==4)
	{
		if(xmlHttp.responseText == "Email Address Is Available")
            document.getElementById("vemail").innerHTML="<span class='Bluematter'>"+xmlHttp.responseText+"</span>";
        else
            document.getElementById("vemail").innerHTML="<span class='Redmatter'>"+xmlHttp.responseText+"</span>";		
			document.getElementById("chk_mail").value = "Check Availability";
	}	
}
//====================================================================================================
//	Function Name	:instrutction(id)
//	Created By: Amit Sankhala
//	Created On: 11 Dec 2007
//	Last Modified By:
//	Last modified On:
//  Purpose : Check wheather states already exist in the tblStates table.
//  Parameters: id : Id of the control to whom Instruction Need to set.
//----------------------------------------------------------------------------------------------------
 
function instrutction(id)
{
	if(document.getElementById(id).value == '')
	{
		if(id = "txtUsername")
		{
			document.getElementById(id).value = "UserName Must Be 4 Characters Long";
		}
	}
}
//====================================================================================================
//	Function Name	:check_state_exist()
//	Created By: Amit Sankhala
//	Created On: 10 Dec 2007
//	Last Modified By:
//	Last modified On:
//  Purpose : Check wheather states already exist in the tblStates table.
//  Parameters: frm : Form name for which variables need to validate.
//----------------------------------------------------------------------------------------------------
 
function check_state_exist(frm)
{
	other_state = frm.other_state.value;	
	xmlHttp_state=GetXmlHttpObject();
	if(xmlHttp_state==null)
	{
		alert ("Your Browser Does Not Support AJAX!");
		return;
	} 
	var newurl="check_state.php";
	newurl=newurl+"?valstate="+other_state;
	xmlHttp_state.onreadystatechange=newstateChanged2;
	xmlHttp_state.open("GET",newurl,true);
	xmlHttp_state.send(null);

}
function newstateChanged2() 
{ 
	if (xmlHttp_state.readyState==4)
	{
		if(xmlHttp_state.responseText == "Exists")
        {
			alert("This State Already Exist in the state DropDown, Please select from State DropDown");
			document.getElementById("idState").focus();
			return false;
		}
		else
		{
			return true;
		}
	}	
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		xmlHttp=new XMLHttpRequest();//Firefox, Opera 8.0+, Safari
	}
	catch(e)
	{
		try//Internet Explorer
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}


//====================================================================================================
//	Function Name	:	Form_Submit()
//	Created By: Maulik Chandarana
//	Created On: 11 June 2007
//	Last Modified By:
//	Last modified On:
//  Purpose : Validates form variables 
//  Parameters: frm : Form name for which variables need to validate.
//----------------------------------------------------------------------------------------------------
function Form_Submit(frm)
{
	with(frm)
    {		
    	if(!IsEmpty(txtUsername, 'Please Enter The Username.'))
        {
            return false;
        }
        if(Trim(txtUsername.value).length < 4)
        {
            alert("Username Should Not Be Less Than 4 Characters");
            txtUsername.focus();
            return false;
        }
        if(!fnCheckForBlankSpace(txtUsername, 'Please Remove Any Blank Space From Username'))
        {
            return false;
        }
		var regex = /^[a-zA-Z0-9-_.]+$/ ;
		if(!regex.test(txtUsername.value)) 
		{
				alert("Username can contain only letters, numbers, periods (.), hyphens (-), and underscores (_). Special characters or accented letters are not allowed. Please type a different UserName");
				txtUsername.focus();
				return false;
		}
        if(!IsEmpty(txtPassword, 'Please Enter The Password.'))
        {
            return false;
        }
        if(Trim(txtPassword.value).length < 4)
        {
            alert("Password Should Not Be Less Than 4 Characters");
            txtPassword.focus();
            return false;
        }
        if(!fnCheckForBlankSpace(txtPassword, 'Please Remove Any Blank Space From Password'))
        {
            return false;
        }
        if(!IsEmpty(txtConfirmPassword, 'Please Enter Confirm Password.'))
        {
            return false;
        }
        if(txtPassword.value != txtConfirmPassword.value)
        {
            alert("Please Enter The Confirm Password Same As Password");
            txtConfirmPassword.focus();
            return false;
        }
        if(!IsEmpty(txtEmail, 'Please Enter The Email Address.'))
        {
            return false;
        }
        if(!IsEmail(txtEmail,'Please Enter Valid Email Address.'))
        {
            return false;
        }
        if(!IsEmpty(txtFirstName, 'Please Enter Your First Name.'))
        {
            return false;
        }
        if(!IsEmpty(txtLastName, 'Please Enter Your Last Name.'))
        {
            return false;
        }		
        /*if(!IsEmpty(txtAddress1, 'Please Enter Your Address.'))
        {
            return false;
        }   */     
        if(optCountry.value == "")
        {
            alert("Please Select Country");
            optCountry.focus();
            return false;
        } 
		
		//====================================================================================================
		//	Created By: Srikanta Biswas
		//	Created On: 03 Sept 2007
		//	Last Modified By: 
		//	Last modified On: 
		//  Purpose: Validates state and city variables(Both is Mozilla Firefox and IE)
		//----------------------------------------------------------------------------------------------------
        if(Trim(document.getElementById("idState").value) == "")
        {
            alert("Please Select State Or Enter The Value Of State");
            document.getElementById("idState").focus();
            return false;
        }
		if(Trim(document.getElementById("idState").value) == "other")
        {
            if(!IsEmpty(other_state, 'Please Enter Other State.'))
			{
				return false;
			}
        }
		if(Trim(document.getElementById("idState").value) == "other")
        {
            var regex = /^[a-zA-Z ]+$/ ;
			if(!regex.test(other_state.value)) 
			{
					alert("Please Enter Valid State Name");
					other_state.focus();
					return false;
			}
        }
        if(Trim(document.getElementById("idState").value) != "")
        {
            hdStateVal.value = document.getElementById("idState").value;
        }
		
        /*if(Trim(document.getElementById("idCity").value) == "")
        {
            alert("Please Select City Or Enter The Value Of City");
            document.getElementById("idCity").focus();
            return false;
        }
        if(Trim(document.getElementById("idCity").value) != "")
        {
            hdCityVal.value = document.getElementById("idCity").value;
        }*/
        if(!IsEmpty(txtZipCode, 'Please Enter Zip Code.'))
        {
            return false;
        }
        /*if(!IsValidZipcode(txtZipCode, 'Please Enter Zipcode In Proper Format.'))
        {
            return false;
        } */       
        if(optSex.value == "")
        {
            alert("Please Select Sex");
            optSex.focus();
            return false;
        }        
        /*if(optMaritalStatus.value == "")
        {
            alert("Please Select Marital Status");
            optMaritalStatus.focus();
            return false;
        }*/
        if(optMonth.value == "")
        {
            alert("Please Select The Month");
            optMonth.focus();
            return false;
        }
        if(optDate.value == "")
        {
            alert("Please Select the Day");
            optDate.focus();
            return false;
        }
        if(optYear.value == "")
        {
            alert("Please Select The Year");
            optYear.focus();
            return false;
        }
        var Month = optMonth.value;
        var Day = optDate.value;
        var Year = optYear.value;
        var BirthDate;
        BirthDate = Year+"-"+Month+"-"+Day;
        var CurrentDate = getTodaysDate();
		//====================================================================================================
		//	Created By: Srikanta Biswas
		//	Created On: 06 Sept 2007
		//	Last Modified By: 
		//	Last modified On: 
		//  Purpose: Validates days based on selected month and year(whether it's leap year or not)
		//----------------------------------------------------------------------------------------------------
		if(Year%100 != "0")
		{
			if(Year%4 == "0")
			{
				if( (Month=="02" && Day=="30") || (Month=="02" && Day=="31") || (Month=="04" && Day=="31") || (Month=="06" && Day=="31") || (Month=="09" && Day=="31") || (Month=="11" && Day=="31") )
				{
					alert("Please Select Proper Day Of The Selected Month");
					return false;
				}
			}
			else
			{
				if( (Month=="02" && Day=="29") || (Month=="02" && Day=="30") || (Month=="02" && Day=="31") || (Month=="04" && Day=="31") || (Month=="06" && Day=="31") || (Month=="09" && Day=="31") || (Month=="11" && Day=="31") )
				{
					alert("Please Select Proper Day Of The Selected Month");
					return false;
				}
			}
		}
		else if(Year%400 == "0")
		{
				if( (Month=="02" && Day=="30") || (Month=="02" && Day=="31") || (Month=="04" && Day=="31") || (Month=="06" && Day=="31") || (Month=="09" && Day=="31") || (Month=="11" && Day=="31") )
				{
					alert("Please Select Proper Day Of The Selected Month");
					return false;
				}
		}
		if(BirthDate > CurrentDate)
        {
            alert("Birth Date Should Not Be Greater Than Today");
            optMonth.focus();
            return false;
        }
        arrDate1 = CurrentDate.split("-"); // split date1
        arrDate2 = BirthDate.split("-"); // split date2
        strMonth1 = (parseInt(arrDate1[1])-1) // get month out of date1 i.e 0-11
        strMonth2 = (parseInt(arrDate2[1])-1) // get month out of date2 i.e 0-11
        objDate1 = new Date(arrDate1[0], strMonth1, arrDate1[2]); // create new object of date1 in unix format
        objDate2 = new Date(arrDate2[0], strMonth2, arrDate2[2]); // create new object of date2 in unix format
    
        intDayDiff = (objDate1-objDate2) // in miliseconds
        intDayDiff = (intDayDiff/(24*60*60*1000)) // in actual days
        intYearDiff = (parseInt(intDayDiff/365)) // in actual Years
        if(intYearDiff < 16)
        {
            alert("Individuals Under The Age Of 16 Are Not Allowed To Register For JoePopular.com. Please See Our Terms.");
            optMonth.focus();
            return false;
        }
        if(!IsEmpty(txtAuthenticateUser, 'Please Enter The Verification Code Shown In Image.'))
        {
            return false;
        }                
        if(chkAgreement.checked == false)
        {
            alert("You Must Agree To Our Terms Of Services And Privacy Policy Before Proceeding.");
            chkAgreement.focus();
            return false;
        }        
        hdMode.value = "Register";
        submit();
    }
}
var url = "getTextVal.php?country_id=";
var isWorking = false;
var http = getHTTPObject();



//====================================================================================================
//    Function Name    :    funzip()
//    Created By: Srikanta Biswas
//    Created On: 06 Sept 2007
//    Last Modified By:
//    Last modified On:
//    Purpose : To allow only numeric values in zip code field
//    Parameters: frm : Form name for which variables need to validate.
//----------------------------------------------------------------------------------------------------
function funzip(frm)
{
	x=frm.txtZipCode.value;
	for(i=0;i<x.length;i++)
	{
		ch=x.charAt(i);
		if(!(ch>='0' && ch<='9'))
		{
			frm.txtZipCode.value=x.substr(0,x.indexOf(ch));
		}
	}
} 
//====================================================================================================
//    Function Name    :    Click_Change_Country()
//    Created By: Maulik Chandarana
//    Created On: 14 August 2007
//    Last Modified By:
//    Last modified On:
//    Purpose : To show the state drop down for selected country name
//    Parameters: intCountryId : Country Id.
//----------------------------------------------------------------------------------------------------
function Click_Change_Country(intCountryId)
{    
    document.getElementById("iLoadStateImage").style.display = '';
    document.getElementById("iState").innerHTML = "";
    
    if (!isWorking)
    {
        if (window.XMLHttpRequest)
        {            
            http = new XMLHttpRequest();
            http.open("GET", url + intCountryId, true);
            isWorking = true;
            http.onreadystatechange = handleHttpResponse;                        
            http.send(null);
            // branch for IE/Windows ActiveX version
        }
        else if (window.ActiveXObject)
        {            
            http = new ActiveXObject("Microsoft.XMLHTTP");
            if (http)
            {                                
                http.open("GET", url + intCountryId, true);
                isWorking = true;                
                http.onreadystatechange = handleHttpResponse;
                http.send();
            }
        }
    }
}
//====================================================================================================
//    Function Name    :    handleHttpResponse()
//    Created By: Maulik Chandarana
//    Created On: 14 August 2007
//    Last Modified By:
//    Last modified On:
//    Purpose : Getting values from XML data for state drop down
//    Parameters:
//----------------------------------------------------------------------------------------------------
function handleHttpResponse()
{    
    if (http.readyState == 4)
    {
        isWorking = false;       
        if (http.responseText.indexOf('invalid') == -1)
        {
            var xmlDocument = http.responseXML;            			
            var number = xmlDocument.getElementsByTagName('state').length;            
            if(number > 0)
            {
                var strHtml = "";
                for(i=0;i<number;i++)
                {                
                    var intVal = xmlDocument.getElementsByTagName('stateid').item(i).firstChild.data;
                    var strState = xmlDocument.getElementsByTagName('state').item(i).firstChild.data;
                    strHtml+= "<option value="+intVal+">"+strState+"</option>";
                    isWorking = false;
                }            
                document.getElementById("iState").innerHTML+= "<select name='optState' id='idState' class='Textarea' onchange='return Click_Change_State(this.value);'><option value=''>--Select State--</option>"+strHtml+"<option value='other'>Other</option></select>";
            }
            else if(number == 0)
            {
                //document.getElementById("iCity").innerHTML = "";
                document.getElementById("iState").innerHTML+= "<input type='text' name='optState' id='idState' class='Textarea' size='20' maxlength='255' />";
                //document.getElementById("iCity").innerHTML+= "<input type='text' name='optCity' id='idCity' class='Textarea' size='20' maxlength='255' />";                                                                                                        
            }
            document.getElementById("iLoadStateImage").style.display = 'none'; 
            //document.getElementById("idState").focus();
        }
    }
}
//====================================================================================================
//    Function Name    :    Click_Change_State()
//    Created By: Maulik Chandarana
//    Created On: 14 August 2007
//    Last Modified By:
//    Last modified On:
//    Purpose : To show the city drop down for selected country and staes names
//    Parameters: intStateId : State Id.
//----------------------------------------------------------------------------------------------------
function Click_Change_State(intStateId)
{    
	if(intStateId == 'other')
	{
		document.getElementById("other_state").style.display = "";	
		document.getElementById("other_label").style.display = "";
	}
	else
	{
		document.getElementById("other_state").style.display = "none";	
		document.getElementById("other_label").style.display = "none";
	}
    /*document.getElementById("iLoadCityImage").style.display = '';
    document.getElementById("iCity").innerHTML = "";
    intCountryId = document.frmRegistration.optCountry.value;
    
    if (!isWorking)
    {
        if (window.XMLHttpRequest)
        {            
            http = new XMLHttpRequest();
            http.open("GET", url + intCountryId + "&state_id=" + intStateId, true);
            isWorking = true;
            http.onreadystatechange = handleHttpResponse1;                        
            http.send(null);
            // branch for IE/Windows ActiveX version
        }
        else if (window.ActiveXObject)
        {            
            http = new ActiveXObject("Microsoft.XMLHTTP");
            if (http)
            {                                
                http.open("GET", url + intCountryId + "&state_id=" + intStateId, true);
                isWorking = true;                
                http.onreadystatechange = handleHttpResponse1;
                http.send();
            }
        }
    }*/
}
//====================================================================================================
//    Function Name    :    handleHttpResponse1()
//    Created By: Maulik Chandarana
//    Created On: 14 August 2007
//    Last Modified By:
//    Last modified On:
//    Purpose : Getting values from XML data for city drop down
//    Parameters:
//----------------------------------------------------------------------------------------------------
function handleHttpResponse1()
{    
    if (http.readyState == 4)
    {
        isWorking = false;
        
        if (http.responseText.indexOf('invalid') == -1)
        {
            var xmlDocument = http.responseXML;            
            var number = xmlDocument.getElementsByTagName('city').length;            
            if(number > 0)
            {
                var strHtml = "";
                for(i=0;i<number;i++)
                {                
                    var intVal = xmlDocument.getElementsByTagName('cityid').item(i).firstChild.data;
                    var strCity = xmlDocument.getElementsByTagName('city').item(i).firstChild.data;
                    strHtml+= "<option value="+intVal+">"+strCity+"</option>";
                    isWorking = false;
                }            
                document.getElementById("iCity").innerHTML+= "<select name='optCity' id='idCity' class='Textarea'><option value=''>--Select City--</option>"+strHtml+"</select>";
            }
            else if(number == 0)
            {
                document.getElementById("iCity").innerHTML+= "<input type='text' name='optCity' id='idCity' class='Textarea' size='20' maxlength='255' />";
            }
            document.getElementById("iLoadCityImage").style.display = 'none';
            document.getElementById("idCity").focus();
        }
    }
}
//====================================================================================================
//	Function Name	:show_instr()
//	Created By: Amit Sankhala
//	Created On: 3 Dec 2007
//	Last Modified By:
//	Last modified On:
//  Purpose : Toggle Display of Instructions.
//  Parameters: id : id of the Toggled form control.
//				status: status show or not.(i.e. display while onfocus and donot display while lost focus.
//----------------------------------------------------------------------------------------------------
function show_instr(id,status)
{
		if(status == 1)
		{
			// display
			document.getElementById(id).style.display = "";
		}
		else
		{
			// donot display
			document.getElementById(id).style.display = "none";
		}		
}


