﻿/*
* Author & Date/Time: Fahad Batla - 16/09/2008
*/

var refreshIntervalId;
var lastRefreshIntervalId = 0;

function ShowStateByCountry(countryId)
{
    document.getElementById('imgWait2').style.display='block';
    
    var ddlState = document.getElementById("ddlState");	
	ddlState.options.length = 0;
    
    AF.ShowCountryStates(countryId, ShowStateByCountry_Callback);
}

function ShowStateByCountry_Callback(res)
{
    document.getElementById('imgWait2').style.display='none';
    
	if (res.error != null)
	{
		alert("ERROR: " + res.error.Message);
		return;
	}
	
	var ddlState = document.getElementById("ddlState");		

	if (res.value != null)
	{	    
		var arrStates = res.value.split("|");
		ddlState[0] = new Option("Please Select...",0, true, true);
		for (i=0,j=1; i<arrStates.length; i++,j++)
		{
			ddlState[j] = new Option(arrStates[i],arrStates[i], false, false);
		}
	}
	else
	{
		ddlState[0] = new Option("N/A", 0, true, true);		
	}
}

function ShowCasteByReligion(relID)
{
    document.getElementById('imgWait').style.display='block';
    
    var ddlCaste = document.getElementById("ddlCaste");
	ddlCaste.disabled = true;
	ddlCaste.options.length = 0;
    
    AF.ShowReligionCaste(relID, ShowCasteByReligion_Callback);
}

function ShowCasteByReligion_Callback(res)
{
    document.getElementById('imgWait').style.display='none';
    
	if (res.error != null)
	{
		alert("ERROR: " + res.error.Message);
		return;
	}
	
	var ddlCaste = document.getElementById("ddlCaste");	
	ddlCaste.disabled = false;

	if (res.value != null)
	{
		var arrCaste = res.value.split("|");
		ddlCaste[0] = new Option("Please Select...",0, true, true);
		for (i=0,j=1; i<arrCaste.length; i++,j++)
		{
			ddlCaste[j] = new Option(arrCaste[i],arrCaste[i], false, false);
		}
	}
	else
	{
		ddlCaste[0] = new Option("N/A", 0, true, true);
		ddlCaste.disabled = true;
	}	
}

/*function LoadStates()
{
    var ddlState = document.getElementById('ddlState');
    ddlState.disabled = false;
}*/

function PutFullName() 
{   
    document.getElementById('lblFullName').innerHTML = document.getElementById('txtFName').value + ' ' + document.getElementById('txtLName').value;
}

function ValidateForm()
{
    var doc = document.frm;

    PutFullName();
	
	if (CheckEmailFormat(doc.txtEmail.value) == false)
	{
		ShowError("Email",1, "Invalid email format.");
		//doc.txtEmail.focus();
		return false;
	}
	else
	    ShowError("Email",0,"");
	
	
	if (doc.txtPassword.value == "")
	{
	    ShowError("Password",1, "Password cannot be left blank.");
	    //doc.txtPassword.focus();	    
		return false;
	}
	else if(doc.txtPassword.value.length < 5 || doc.txtPassword.value.length > 15)
    {		
		ShowError("Password",1, "Password length must be min 5 chars & max 15 chars.");
		//doc.txtPassword.focus();
		return false;
    }
	else
	    ShowError("Password",0,"");
	    	
	if (doc.txtFName.value == "")
	{		
		ShowError("FName",1, "Please enter first name.");
		//doc.txtFName.focus();
		return false;
	}
	else
	    ShowError("FName",0,"");
	
	if (doc.txtLName.value == "")
	{
		ShowError("LName",1, "Please enter last name.");
		//doc.txtLName.focus();
		return false;
	}
	else
	    ShowError("LName",0,"");
	
	if (doc.ddlGender.selectedIndex == 0)
	{
		ShowError("Gender",1, "Please select gender.");
		//doc.ddlGender.focus();
		return false;
	}
	else
	    ShowError("Gender",0, "");
	
	if(doc.ddlBirthMonth.selectedIndex == 0)
	{
		ShowError("BirthDate",1, "Please select month of your birth.");
		//doc.ddlBirthMonth.focus();
		return false;
	}
	else
	    ShowError("BirthDate",0, "");
	
	if(doc.ddlBirthDay.selectedIndex == 0)
	{		
		ShowError("BirthDate",1, "Please select day of your birth.");
		//doc.ddlBirthDay.focus();
		return false;
	}
	else
	    ShowError("BirthDate",0, "");
	
	if(doc.ddlBirthYr.selectedIndex == 0)
	{		
		ShowError("BirthDate",1, "Please select years of your birth.");
		//doc.ddlBirthYr.focus();
		return false;
	}
	else
	    ShowError("BirthDate",0, "");
	
	if (doc.ddlReligion.selectedIndex == 0)
	{		
		ShowError("Religion",1, "Please select religion.");
		//doc.ddlReligion.focus();
		return false;
	}
	else
	{
	    ShowError("Religion",0, "");
		if (doc.ddlCaste.disabled == false && doc.ddlCaste.selectedIndex == 0)
		{			
			ShowError("Caste",1, "Please select caste.");
			//doc.ddlCaste.focus();
			doc.hidCasteVal.value = 0;
			return false;
		}
		else
		{
		    ShowError("Caste",0, "");
		    doc.hidCasteVal.value = doc.ddlCaste.value;
		}		
	}
	
	if (doc.txtAddress1.value == "")
	{		
		ShowError("Address1",1, "Please enter address.");
		//doc.txtAddress1.focus();
		return false;		
	}
	else
	    ShowError("Address1",0, "");	    
	
	if (doc.txtCity.value == "")
	{		
		ShowError("City",1, "Please enter city.");
		//doc.txtCity.focus();
		return false;
	}
	else
	    ShowError("City",0, "");
	
	if (doc.txtZip.value == "")
	{
		ShowError("Zip",1, "Please enter zip.");
		//doc.txtZip.focus();
		return false;
	}
	else
	    ShowError("Zip",0, "");
	   
	
	if (doc.ddlCountry.selectedIndex == 0)
	{		
		ShowError("Country",1, "Please select country.");		
		return false;
	}
	else
	{
	    ShowError("Country",0, "");
		if (doc.ddlState.selectedIndex == 0)
		{			
			ShowError("State",1, "Please select state/province.");			
			doc.hidStateVal.value = 0;
			return false;
		}
		else
		{
		    ShowError("State",0, "");
		    doc.hidStateVal.value = doc.ddlState.value;
		}
	}	

	if (doc.txtCell.value == "")
	{	
		ShowError("Cell",1, "Please enter Cell/Mobile Number.");
		//doc.txtPhoneDay.focus();
		return false;
	}
	else if(isNaN(doc.txtCell.value))
	{
	    ShowError("Cell",1, "Only digits are allowed.");
		//doc.txtPhoneDay.focus();
		return false;
	}
	else
	    ShowError("Cell",0, "");
	
	if (doc.txtPhoneEve.value != "" && isNaN(doc.txtPhoneEve.value))
	{
		ShowError("PhoneEve",1, "Only digits are allowed.");
		//doc.txtPhoneEve.focus();
		return false;
	}
	else
	    ShowError("PhoneEve",0, "");
	
	if (doc.txtCell.value != "" && isNaN(doc.txtCell.value))
	{
		ShowError("Cell",1, "Only digits are allowed.");
		//doc.txtCell.focus();
		return false;		
	}
	else
	    ShowError("Cell",0, "");
		
	if (doc.ddlHeight.selectedIndex == 0)
	{
		ShowError("Height",1, "Please select height.");
		//doc.ddlHeight.focus();
		return false;		
	}
	else
	    ShowError("Height",0, "");
	
	if (doc.ddlMaritalStatus.selectedIndex == 0)
	{
		ShowError("MaritalStatus",1, "Please select marital status.");
		//doc.ddlMaritalStatus.focus();
		return false;		
	}
	else
	    ShowError("MaritalStatus",0, "");
		
	if (doc.ddlEducation.selectedIndex == 0)
	{
		ShowError("Education",1, "Please select education.");
		//doc.ddlEducation.focus();
		return false;		
	}
	else
	    ShowError("Education",0, "");
	
	if (doc.txtOccupation.value == "")
	{
		ShowError("Occupation",1, "Please tell us your occupation.");		
		return false;		
	}
	else
	    ShowError("Occupation",0, "");
	
	if (doc.ddlNationality.selectedIndex == 0)
	{
		ShowError("Nationality",1, "Please select nationality.");
		//doc.ddlNationality.focus();
		return false;		
	}
	else
	    ShowError("Nationality",0, "");
	
	if (doc.ddlVisaType.selectedIndex == 0)
	{
		ShowError("VisaType",1, "Please select visa type.");
		//doc.ddlVisaType.focus();
		return false;		
	}
	else
	    ShowError("VisaType", 0, "");

	if (doc.ddlFamilyOrigin.selectedIndex == 0) {
	    ShowError("FamilyOrigin", 1, "Please select family origin.");	    
	    return false;
	}
	else
	    ShowError("FamilyOrigin", 0, "");

	if (doc.ddlBirthPlace.selectedIndex == 0) {
	    ShowError("BirthPlace", 1, "Please select place of birth.");
	    return false;
	}
	else
	    ShowError("BirthPlace", 0, "");
	
	if (doc.ddlSkinColor.selectedIndex == 0)
	{
		ShowError("SkinColor",1, "Please select skin color.");
		//doc.ddlSkinColor.focus();
		return false;
	}
	else
	    ShowError("SkinColor",0, "");
	
	if (doc.ddlIncomeLevel.selectedIndex == 0)
	{
		ShowError("IncomeLevel",1, "Please select income level.");
		//doc.ddlIncomeLevel.focus();
		return false;
	}
	else
	    ShowError("IncomeLevel",0, "");
	
	if (doc.ddlSmoke.selectedIndex == 0)
	{
		ShowError("Smoke",1, "Please select smoking habit.");
		//doc.ddlSmoke.focus();
		return false;		
	}
	else
	    ShowError("Smoke",0, "");
	
	if (doc.ddlDrink.selectedIndex == 0)
	{
		ShowError("Drink",1, "Please select drinking habit.");
		//doc.ddlDrink.focus();
		return false;		
	}
	else
	    ShowError("Drink",0, "");
	    
	if (doc.ddlBuilt.selectedIndex == 0)
	{
		ShowError("Built",1, "Please select built.");		
		return false;		
	}
	else
	    ShowError("Built",0, "");
	    
	if (doc.ddlWeight.selectedIndex == 0)
	{
		ShowError("Weight",1, "Please select weight.");		
		return false;		
	}
	else
	    ShowError("Weight",0, "");
		
	if (doc.ddlLanguageSpeak.selectedIndex == -1)
	{
		ShowError("Languages",1, "Please select atleast 1 language.");
		//doc.ddlLanguageSpeak.focus();
		return false;		
	}
	else
	    ShowError("Languages",0, "");
	
	if (doc.txtAboutMyself.value == "")
	{
		ShowError("Desc1",1, "Please tell something about yourself.");
		//doc.txtAboutMyself.focus();
		return false;		
	}
	else
	    ShowError("Desc1",0, "");
	
	if (doc.txtLifePartner.value == "")
	{
		ShowError("Desc2",1, "Please tell us something about your ideal life partner");
		//doc.txtLifePartner.focus();
		return false;		
	}
	else
	    ShowError("Desc2",0, "");
	
	if (doc.ddlDesiredAgeFrom.value > doc.ddlDesiredAgeTo.value)
	{
		ShowError("PartnerAge",1, "Desired age range of your partner is wrong.");
        //doc.ddlDesiredAgeFrom.focus();
		return false;
	}
	else
	    ShowError("PartnerAge",0, "");
	    
	if (doc.ddlHearAboutUs.selectedIndex == 0)
	{
		ShowError("Heard",1, "Please tell us where did you hear about us.");		
		return false;		
	}
	else
	    ShowError("Heard",0, "");	
	    
	if (doc.cbTerms.checked == false)
	{
		ShowError("ToS",1, "You must agree to the Terms of Service.");		
		return false;		
	}
	else
	    ShowError("ToS", 0, "");

	if (doc.cbInfoTrue.checked == false) {
	    ShowError("InfoTrue", 1, "You must certify the information given here.");
	    return false;
	}
	else
	    ShowError("InfoTrue", 0, "");	
	
	return true;
}

function CheckEmailFormat(eAdd)
{
	var rate = eAdd.indexOf("@");
	if(rate == -1)
		return false;
	else
	{
		var dot = eAdd.indexOf(".");
		if(dot == -1)
			return false;
		else
			return true;
	}
}


function containsInvalidChars(txtValue)
{
	var validChars = "1234567890-";
	var myBool = false;

	for(i=0; i<txtValue.length; i++)
	{
		temp = txtValue.charAt(i);
		if (validChars.indexOf(temp) > -1)
			continue;
		else
		{
			myBool = true;
			break;
		}
	}
	return myBool;
}

function ShowError(controlName, showHide, msg)
{    
    var tr1 = document.getElementById("trErrorTop");
    var spn1 = document.getElementById("spnErrorTop");
    var tr2 = document.getElementById("trErrorBot");
    var spn2 = document.getElementById("spnErrorBot");
    var trControl = document.getElementById("tr" + controlName);
        
    if(showHide == 1)
    {
        if(lastRefreshIntervalId != refreshIntervalId)
        {
            trControl.style.backgroundColor = "#FFD7E6";
            tr1.style.display = "block";
            spn1.innerHTML = "<b>" + msg  + "</b><i> Scroll down to see (highlighted)</i>";
            tr2.style.display = "block";
            spn2.innerHTML = "<b>" + msg  + "</b><i> Scroll up to see (highlighted)</i>";
            refreshIntervalId = setInterval("MyBlink(\'tr"+ controlName +"\')",500);
            lastRefreshIntervalId = refreshIntervalId;
        }
    }
    else
    { 
        clearInterval(refreshIntervalId);
        lastRefreshIntervalId = 0;
        trControl.style.backgroundColor = "";
        tr1.style.display = "none";
        spn1.innerHTML = "";
        tr2.style.display = "none";
        spn2.innerHTML = "";
    }
}

function MyBlink(s)
{   
    var obj = document.getElementById(s);
    if(obj.style.backgroundColor == "#ffd7e6")
        obj.style.backgroundColor = "";
    else
        obj.style.backgroundColor = "#ffd7e6";
}



