﻿// JScript File

var xmlHttp;

// Call an aspx page that creates the information in Html format and return it in a string (Response.Write(string)).
function CheckUsernameAvilable()
{ 
    var userName = document.getElementById("ctl00_ContentPlaceHolder1_txtUsername");
    
    if(userName == null || userName.value == "" || !CheckUsernameValid(userName.value))
	{
	    document.getElementById("ctl00_ContentPlaceHolder1_lblUserAvailableMsg").style.color = "Red";
	    document.getElementById("ctl00_ContentPlaceHolder1_lblUserAvailableMsg").innerHTML = "Username invalid";
	    return;
	}
	////document.getElementById("ctl00_ContentPlaceHolder1_lblUserAvailableMsg").innerHTML = "";
	
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    } 
    var url="GetInfo.aspx";
    url=url+"?Username="+userName.value;
    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

// When the aspx file is done creating the string, it writes it to the output (xmlHttp gets it).
function stateChanged() 
{ 
    if (xmlHttp.readyState==4)
    { 
        if(xmlHttp.responseText.search('Unavailable') == -1) 
        {
            document.getElementById("ctl00_ContentPlaceHolder1_lblUserAvailableMsg").style.color = "Green";
	        document.getElementById("ctl00_ContentPlaceHolder1_lblUserAvailableMsg").innerHTML = "Username available!";
        }
        else
        {
            document.getElementById("ctl00_ContentPlaceHolder1_lblUserAvailableMsg").style.color = "Red";
	        document.getElementById("ctl00_ContentPlaceHolder1_lblUserAvailableMsg").innerHTML = "Username unavailable";
        } 
    }
}

// Create the xmlHttp Object.
function GetXmlHttpObject()
{
    var xmlHttp=null;
    try
      {
          // Firefox, Opera 8.0+, Safari
          xmlHttp=new XMLHttpRequest();
      }
    catch (e)
      {
          // Internet Explorer
          try
            {
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
            }
          catch (e)
            {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
      }
    return xmlHttp;
}



function ValidateSignIn()
{
    var loginUserName = document.getElementById("ctl00_ContentPlaceHolder1_txtLoginUserName");
    var loginPassword = document.getElementById("ctl00_ContentPlaceHolder1_txtLoginPassword");
    var originalColor = '#666666';
    var loginValid = true;
    var passwordValid = true;
    
    if(loginUserName == null || loginUserName.value == "")
	{
	    document.getElementById("ctl00_ContentPlaceHolder1_lblLoginUserName").style.color = "Red";
	    loginValid = false;
	}
	else
	{
	    document.getElementById("ctl00_ContentPlaceHolder1_lblLoginUserName").style.color = originalColor;
	    loginValid = true;   
	}    
	
	if(loginPassword == null || loginPassword.value == "")
	{
	    document.getElementById("ctl00_ContentPlaceHolder1_lblLoginPassword").style.color = "Red";
	    passwordValid = false;
	}    
	else
	{
	    document.getElementById("ctl00_ContentPlaceHolder1_lblLoginPassword").style.color = originalColor;    
	    passwordValid = true;
	}
	
	if(loginValid == true && passwordValid == true)
	    return true;
	else
	    return false;
}

function ValidateCreateAccount()
{
    var EmailErr = "N", PasswordErr = "N", PasswordVerifyErr = "N", UsernameErr = "N"; // User/Password
    var LocationErr = "N", ZipCodeErr = "N", DOBMonthErr = "N", DOBDayErr = "N", DOBYearErr = "N", GenderErr = "N"; // Personal
    var CompanyNameErr = "N", ProductServiceListErr = "N", ContactNameErr = "N", CityErr = "N", StateErr = "N", ZipErr = "N" // Business
    var SectionErr = "N", CategoryErr = "N", CountryErr = "N", StatesErr = "N"; // Business
    var ImgVerifyErr = "N", AcceptTermsErr = "N";
     
    // Use Prefix since we use master.
    var txtPrefix = "ctl00$ContentPlaceHolder1$"; 
    var lblPrefix = "ctl00_ContentPlaceHolder1_";
  
    var originalColor = document.getElementById("lblUserExample").style.color; // Assume we don't change the color of this label.
    //var accountType = document.getElementById(lblPrefix + "cmbAccountType");
    var email = document.getElementById(lblPrefix + "txtEmail");
    var password = document.getElementById(lblPrefix + "txtPassword");
    var rePassword = document.getElementById(lblPrefix + "txtRePassword");
    var username = document.getElementById(lblPrefix + "txtUsername");
//    var location = document.getElementById(lblPrefix + "cmbLocation");
//    var zipCode = document.getElementById(lblPrefix + "txtZipCode");
//    var dobMonth = document.getElementById(lblPrefix + "cmbDOBMonth");
//    var dobDay = document.getElementById(lblPrefix + "cmbDOBDay");
//    var dobYear = document.getElementById(lblPrefix + "cmbDOBYear");
//    var genderMale = document.getElementById(lblPrefix + "radioGenderMale");
//    var genderFemale = document.getElementById(lblPrefix + "radioGenderFemale");
    
    ///////// Business
//    var companyName = document.getElementById(lblPrefix + "txtCompanyName");
//    var productServiceList = document.getElementById(lblPrefix + "txtProductServiceList");
//    var contactName = document.getElementById(lblPrefix + "txtContactName");
//    var city = document.getElementById(lblPrefix + "txtCity");
//    var state = document.getElementById(lblPrefix + "cmbState");
//    var zip = document.getElementById(lblPrefix + "txtZip");
//    var section = document.getElementById(lblPrefix + "cmbSection");    
//    var category = document.getElementById(lblPrefix + "cmbCategory");
//    var country = document.getElementById(lblPrefix + "cmbCountry");
    
    
//    if(country.value == 'United States')
//    {
//        var states = document.getElementById(lblPrefix + "cmbUSAStates");
//    }
//    else
//    {
//        var states = document.getElementById(lblPrefix + "cmbCanadaStates");
//    }
    
    var imgVerify = document.getElementById(lblPrefix + "txtImgVerify");
    var acceptTerms = document.getElementById(lblPrefix + "chkAcceptTerms");
    var createAccountMessage = document.getElementById(lblPrefix + "lblCreateAccountMessage");
    
    
    if(email == null || email.value == "" || IsEmail(email.value) == false)
	{
	    document.getElementById(lblPrefix + "lblEmail").style.color = "Red";
	    EmailErr = "Y";
	}
	else
	{
	    document.getElementById(lblPrefix + "lblEmail").style.color = originalColor;
	    EmailErr = "N";
	}
	if(password == null || password.value == "" || CheckPasswordValid(password.value) == false)
	{
	    document.getElementById(lblPrefix + "lblPassword").style.color = "Red";
	    PasswordErr = "Y";
	}    
	else
	{
	    document.getElementById(lblPrefix + "lblPassword").style.color = originalColor;    
	    PasswordErr = "N";
	}
	if(rePassword == null || rePassword.value == "" || rePassword.value != password.value) 
	{
	    document.getElementById(lblPrefix + "lblRePassword").style.color = "Red";
	    PasswordVerifyErr = "Y";
	}    
	else
	{
	    document.getElementById(lblPrefix + "lblRePassword").style.color = originalColor;    
	    PasswordVerifyErr = "N";
	}
	if(username == null || username.value == "" || CheckUsernameValid(username.value) == false)
	{
	    document.getElementById(lblPrefix + "lblUsername").style.color = "Red";
	    UsernameErr = "Y";
	}
	else
	{
	    document.getElementById(lblPrefix + "lblUsername").style.color = originalColor;
	    UsernameErr = "N";
	}
	
//	if(accountType.value == "Personal")
//	{
//	    if(location == null || location.value == "")
//	    {
//	        document.getElementById(lblPrefix + "lblLocation").style.color = "Red";
//	        LocationErr = "Y";
//	    }
//	    else
//	    {
//	        document.getElementById(lblPrefix + "lblLocation").style.color = originalColor;
//	        LocationErr = "N";
//	    }
//	    if(zipCode == null || zipCode.value == "" || IsUSAZip(zipCode.value) == false)
//	    {
//	        document.getElementById(lblPrefix + "lblZipCode").style.color = "Red";
//	        ZipCodeErr = "Y";
//	    }
//	    else
//	    {
//	        document.getElementById(lblPrefix + "lblZipCode").style.color = originalColor;
//	        ZipCodeErr = "N";
//	    }
//	    if(dobMonth == null || dobMonth.value == "" || dobMonth.value == "0")
//	    {
//	        document.getElementById(lblPrefix + "lblDateOfBirth").style.color = "Red";
//	        DOBMonthErr = "Y";
//	    }
//	    else
//	    {
//	        document.getElementById(lblPrefix + "lblDateOfBirth").style.color = originalColor;
//	        DOBMonthErr = "N";
//	    }
//	    if(dobDay == null || dobDay.value == "" || dobDay.value == "0")
//	    {
//	        document.getElementById(lblPrefix + "lblDateOfBirth").style.color = "Red";
//	        DOBDayErr = "Y";
//	    }
//	    else
//	    {
//	        document.getElementById(lblPrefix + "lblDateOfBirth").style.color = originalColor;
//	        DOBDayErr = "N";
//	    }
//	    if(dobYear == null || dobYear.value == "" || dobYear.value == "0")
//	    {
//	        document.getElementById(lblPrefix + "lblDateOfBirth").style.color = "Red";
//	        DOBYearErr = "Y";
//	    }
//	    else
//	    {
//	        document.getElementById(lblPrefix + "lblDateOfBirth").style.color = originalColor;
//	        DOBYearErr = "N";
//	    }
//	    if((genderMale == null || genderMale.checked == false) && (genderFemale == null || genderFemale.checked == false))
//	    {
//	        document.getElementById(lblPrefix + "lblGender").style.color = "Red";
//	        GenderErr = "Y";
//	    }
//	    else
//	    {
//	        document.getElementById(lblPrefix + "lblGender").style.color = originalColor;
//	        GenderErr = "N";
//	    }
//	  
//	    CompanyNameErr = "N";
//	    document.getElementById(lblPrefix + "lblCompanyName").style.color = originalColor;
//	    ProductServiceListErr = "N";
//	    document.getElementById(lblPrefix + "lblProductServiceList").style.color = originalColor;
//	    ContactNameErr = "N";
//	    document.getElementById(lblPrefix + "lblContactName").style.color = originalColor;
//	    CityErr = "N";
//	    document.getElementById(lblPrefix + "lblCity").style.color = originalColor;
//	    StateErr = "N";
//	    document.getElementById(lblPrefix + "lblState").style.color = originalColor;
//	    ZipErr = "N";
//	    document.getElementById(lblPrefix + "lblZip").style.color = originalColor;
//	    SectionErr = "N";
//	    document.getElementById(lblPrefix + "lblSection").style.color = originalColor;
//	    CategoryErr = "N";
//	    document.getElementById(lblPrefix + "lblCategory").style.color = originalColor;
//	    CountryErr = "N";
//	    document.getElementById(lblPrefix + "lblCountry").style.color = originalColor;
//	    StatesErr = "N";
//	    document.getElementById(lblPrefix + "lblStatesUSA").style.color = originalColor;
//	    document.getElementById(lblPrefix + "lblStatesCanada").style.color = originalColor;
//	    
//	}
//	
//	if(accountType.value == "Business")
//	{
//	    if(companyName == null || companyName.value == "")
//	    {
//	        document.getElementById(lblPrefix + "lblCompanyName").style.color = "Red";
//	        CompanyNameErr = "Y";
//	    }
//	    else
//	    {
//	        document.getElementById(lblPrefix + "lblCompanyName").style.color = originalColor;
//	        CompanyNameErr = "N";
//	    }
//	    
//	    if(productServiceList == null || productServiceList.value == "")
//	    {
//	        document.getElementById(lblPrefix + "lblProductServiceList").style.color = "Red";
//	        ProductServiceListErr = "Y";
//	    }
//	    else
//	    {
//	        document.getElementById(lblPrefix + "lblProductServiceList").style.color = originalColor;
//	        ProductServiceListErr = "N";
//	    }
//	    if(contactName == null || contactName.value == "")
//	    {
//	        document.getElementById(lblPrefix + "lblContactName").style.color = "Red";
//	        ContactNameErr = "Y";
//	    }
//	    else
//	    {
//	        document.getElementById(lblPrefix + "lblContactName").style.color = originalColor;
//	        ContactNameErr = "N";
//	    }
//	    if(city == null || city.value == "")
//	    {
//	        document.getElementById(lblPrefix + "lblCity").style.color = "Red";
//	        CityErr = "Y";
//	    }
//	    else
//	    {
//	        document.getElementById(lblPrefix + "lblCity").style.color = originalColor;
//	        CityErr = "N";
//	    }
//	    if(state == null || state.value == "")
//	    {
//	        document.getElementById(lblPrefix + "lblState").style.color = "Red";
//	        StateErr = "Y";
//	    }
//	    else
//	    {
//	        document.getElementById(lblPrefix + "lblState").style.color = originalColor;
//	        StateErr = "N";
//	    }
//	    if(zip == null || zip.value == "")
//	    {
//	        document.getElementById(lblPrefix + "lblZip").style.color = "Red";
//	        ZipErr = "Y";
//	    }
//	    else
//	    {
//	        document.getElementById(lblPrefix + "lblZip").style.color = originalColor;
//	        ZipErr = "N";
//	    }
//	    if(section == null || section.value == "")
//	    {
//	        document.getElementById(lblPrefix + "lblSection").style.color = "Red";
//	        SectionErr = "Y";
//	    }
//	    else
//	    {
//	        document.getElementById(lblPrefix + "lblSection").style.color = originalColor;
//	        SectionErr = "N";
//	    }
//	    if(category == null || category.value == "")
//	    {
//	        document.getElementById(lblPrefix + "lblCategory").style.color = "Red";
//	        CategoryErr = "Y";
//	    }
//	    else
//	    {
//	        document.getElementById(lblPrefix + "lblCategory").style.color = originalColor;
//	        CategoryErr = "N";
//	    } 
//	    
//	    StatesErr = "N";
//	    document.getElementById(lblPrefix + "lblStatesUSA").style.color = originalColor;
//	    document.getElementById(lblPrefix + "lblStatesCanada").style.color = originalColor;
//	    if(country == null || country.value == "")
//	    {
//	        document.getElementById(lblPrefix + "lblCountry").style.color = "Red";
//	        CountryErr = "Y";
//	    }
//	    else
//	    {
//	        document.getElementById(lblPrefix + "lblCountry").style.color = originalColor;
//	        CountryErr = "N";
//	    } 
//	    if(states == null || states.value == "")
//	    {
//	        if(country.value == 'United States')
//                document.getElementById(lblPrefix + "lblStatesUSA").style.color = "Red";
//            else
//                document.getElementById(lblPrefix + "lblStatesCanada").style.color = "Red";
//            StatesErr = "Y";
//	    }
//	    else
//	    {
//	        document.getElementById(lblPrefix + "lblStatesUSA").style.color = originalColor;
//	        document.getElementById(lblPrefix + "lblStatesCanada").style.color = originalColor;
//	        StatesErr = "N";
//	    } 
//	    
//	    LocationErr = "N";
//	    document.getElementById(lblPrefix + "lblLocation").style.color = originalColor;
//	    ZipCodeErr = "N";
//	    document.getElementById(lblPrefix + "lblZipCode").style.color = originalColor;
//	    DOBMonthErr = "N";
//	    DOBDayErr = "N";
//	    DOBYearErr = "N";
//	    document.getElementById(lblPrefix + "lblDateOfBirth").style.color = originalColor;
//	    GenderErr = "N";
//	    document.getElementById(lblPrefix + "lblGender").style.color = originalColor;
//	}
	
	if(imgVerify == null || imgVerify.value == "")
	{
	    document.getElementById(lblPrefix + "lblImgVerify").style.color = "Red"; 
	    ImgVerifyErr = "Y";
	}
	else
	{
	    document.getElementById(lblPrefix + "lblImgVerify").style.color = originalColor;
	    ImgVerifyErr = "N";
	}
	if(acceptTerms == null || acceptTerms.checked == false)
	{
	    document.getElementById(lblPrefix + "lblAcceptTerms").style.color = "Red"; 
	    AcceptTermsErr = "Y";
	}
	else
	{
	    document.getElementById(lblPrefix + "lblAcceptTerms").style.color = originalColor;
	    AcceptTermsErr = "N";
	}
                                                                                                                                                                                                                                                                                     
	if(EmailErr == "Y" || PasswordErr == "Y" || PasswordVerifyErr == "Y" || UsernameErr == "Y" || LocationErr == "Y" || ZipCodeErr == "Y" || DOBMonthErr == "Y" || DOBDayErr == "Y" || DOBYearErr == "Y" || GenderErr == "Y" || ((CompanyNameErr == "Y" || ProductServiceListErr == "Y" || ContactNameErr =="Y" || CityErr == "Y" || StateErr =="Y" || ZipErr =="Y" || SectionErr == "Y" || CategoryErr =="Y"|| CountryErr =="Y" || StatesErr == "Y") && accountType.value == "Business") || ImgVerifyErr == "Y" || AcceptTermsErr == "Y")
	{
	    document.getElementById(lblPrefix + "lblCreateAccountMessage").style.color = "Red";
        document.getElementById(lblPrefix + "lblCreateAccountMessage").innerHTML = "<b>Please correct the required fields on red !</b>";
	    return false;
	}
	else
	{   
	    document.getElementById(lblPrefix + "lblCreateAccountMessage").style.color = "Green";
	    document.getElementById(lblPrefix + "lblCreateAccountMessage").innerHTML = "<b>Creating Account...</b>";
	    revealModal('divModal');
	    return true;
	}  
}

function CheckUsernameValid(Username)
{
    //var ValidChars = "0123456789."; With Decimal Point !
	var ValidChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var Char; 
	
	if(Username.length == 0)
		return false;
		
	for (i = 0; i < Username.length; i++) 
	{  
		Char = Username.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		    return false;
	}
	
	return true;
}

function CheckPasswordValid(Password)
{
    //var ValidChars = "0123456789."; With Decimal Point !
	var ValidChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var Char; 
	
	if(Password.length == 0)
		return false;
		
	for (i = 0; i < Password.length; i++) 
	{ 
		Char = Password.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		    return false;
	}
	return true;
}

function SelectAccountType()
{
    var accountType = document.getElementById("ctl00_ContentPlaceHolder1_cmbAccountType");
    
    if(accountType == null || accountType.value == "" || accountType.value == "Personal")
	{
	    document.getElementById("trBusinessAccount").style.display = 'none';
	    document.getElementById("trPersonalAccount").style.display = 'block';
	}
	else
	{
	    document.getElementById("trBusinessAccount").style.display = 'block';
	    document.getElementById("trPersonalAccount").style.display = 'none';
	}
}

function SelectCountry()
{
    var country = document.getElementById("ctl00_ContentPlaceHolder1_cmbCountry");
   
    if(country == null || country.value == "" || country.value == "United States")
	{
	    document.getElementById("ctl00_ContentPlaceHolder1_trUSAStates").style.display = 'block';
	    document.getElementById("ctl00_ContentPlaceHolder1_trCanadaStates").style.display = 'none';
	}
	else
	{
	    document.getElementById("ctl00_ContentPlaceHolder1_trUSAStates").style.display = 'none';
	    document.getElementById("ctl00_ContentPlaceHolder1_trCanadaStates").style.display = 'block';
	}
}


function IsUSAZip(Zip)
{
    var len = Zip.length;
    var IsZip = false;
    
	if(len == 5 || (len == 10 && Zip.charAt(5) == '-'))
	{
	    IsZip = true;
		var ValidChars = "0123456789-";
		var Char; 
		for (i = 0; i < len && IsZip == true; i++) 
	    { 
		    Char = Zip.charAt(i);
		    if (ValidChars.indexOf(Char) == -1) 
		    {
			    IsZip = false;
		    }
	    }
	}
	
    return IsZip;
}
