﻿// JScript File

/**********************/
function showOther(rdoValue,serviceDesc)
{
    var divServiceType = document.getElementById("div_srvType");
	if(rdoValue=='138'){
		divServiceType.style.display="block";
		document["frmCarpetCleaning"].txtOtherServiceDesc.focus();
	}
	else
	{
		divServiceType.style.display="none";
	}
}
/**********************/
function validate()
{
	if((document["frmCarpetCleaning"].txtFName.value=="")&&(document["frmCarpetCleaning"].txtLName.value=="")){
		alert("Insert first/last name please")
		document["frmCarpetCleaning"].txtFName.focus()
		return
	}

	if((document["frmCarpetCleaning"].txtPhone.value=="")&&(document["frmCarpetCleaning"].txtEMail.value=="")){
		alert("Insert phone or email address please")
		document["frmCarpetCleaning"].txtPhone.focus()
		return
	}

	/*****/
	var phone = document['frmCarpetCleaning'].txtPhone;
	if(phone.value==""){
		alert("Insert your phone number Please");
		phone.focus();
		return;
	}
	else
	{
		var minNum = 14;
		if(isNaN(phone.value)==false){
			minNum = 10;
		}

		if(phone.value.length!=minNum){
			alert("Please Insert a standard phone number\nMin. 10 digits / xxx-xxx-xxxx format");
			phone.focus();
			return;
		}
	}
	/*****/

	var email = document["frmCarpetCleaning"].txtEMail.value
	if(email!="")
	{
		var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
		if (!regex.test(email))
		{
			alert("Insert correct Email address please");
			document["frmCarpetCleaning"].txtEMail.focus();
			return
			return
		}
	}
	/*****/

	if((document["frmCarpetCleaning"].txtZipCode.value=="")){
		alert("Insert zip code please");
		document["frmCarpetCleaning"].txtZipCode.focus();
		return;
	}


	var ok=0;
	var rdoSelected;
	for(i=0;i<document["frmCarpetCleaning"].rdoServiceType.length;i++){
		if(document["frmCarpetCleaning"].rdoServiceType[i].checked==true){
			ok=1;
			rdoSelected=document["frmCarpetCleaning"].rdoServiceType[i].value
			break;
		}
	}
	
	if(ok==0){
		alert("Choose the service you need Please")
		return;
	}
	else
	{
		if(rdoSelected=='138'){
			if(document["frmCarpetCleaning"].txtOtherServiceDesc.value==''){
				alert("Specify your requested service Please");
				document["frmCarpetCleaning"].txtOtherServiceDesc.focus();
				return;
			}
		}
	}
	/*****/
    document["frmCarpetCleaning"].submit();
}
/**********************/


/*
FormatTelNo method takes in the current element of the form and formats the phone number 
in "(123 456-7890" format. This method should be fired on every key entry 
(using onKeyUp method) in the current element of the form. If any key other than 0 to 9 
is entered, it erases that entry rightaway. Maxlength and size of the current
element of the form should be 13.
Eg. <netui:textBox size="13" maxlength="13" onKeyUp="JavaScript:formatTelNo (this);" onBlur="JavaScript:checkTelNo (this);" onKeyDown="JavaScript:formatTelNo (this);"/>    
*/
function formatTelNo (telNo)
{
    // If it's blank, save yourself some trouble by doing nothing.
    if (telNo.value == "") return;

    var phone = new String (telNo.value);
    
    /*
    "." means any character. If you try to use "(" and ")", the regular expression becomes 
    complicated sice both are reserve characters and escaping them sometimes fails. So just 
    use "." for any character and replace it later.
    */
    if (phone.match (".[0-9]{3}.[0-9]{3}-[0-9]{4}") == null)
    {
        /*
        Following "if" is for user making any changes to the formatted tel. no. If you don't put this 
        "if" condition, the user can not correct a digit by first deleting it and then entering a 
        correct one, since this will fire two "onkeyup" events : first one on deleting a 
        character and second one on entering the correct one. The first "onkeyup" event will fire this 
        function which will reformatt the tel no before the user gets a chace to correct the digit. This 
        will surely confuse the user. The "if" condition below eliminates that.
        */
        if (phone.match (".[0-9]{2}.[0-9]{3}-[0-9]{4}|" + ".[0-9].[0-9]{3}-[0-9]{4}|" +
            ".[0-9]{3}.[0-9]{2}-[0-9]{4}|" + ".[0-9]{3}.[0-9]-[0-9]{4}") == null)
        {
            /*
            You will reach here only if the user is still typing the number or if he/she has 
            messed up already formatted number. 
            */
            var phoneNumeric = phoneChar = "", i;
            // Loop thru what user has entered.
            for (i=0;i<phone.length;i++)
            {
                // Go thru what user has entered one character at a time.
                phoneChar = phone.substr (i,1);
    
                // If that character is not a number or is a White space, ignore it. Only if it is a digit, 
                // concatinate it with a number string.
                if (!isNaN (phoneChar) && (phoneChar != " ")) phoneNumeric = phoneNumeric + phoneChar;
            }
    
            phone = "";
            // At this point, you have picked up only digits from what user has entered. Loop thru it.
            for (i=0;i<phoneNumeric.length;i++)
            {
                // If it's the first digit, throw in "(" before that.
                if (i == 0) phone = phone + "(";
                // If you are on the 4th digit, put ") " before that.
                if (i == 3) phone = phone + ") ";
                // If you are on the 7th digit, insert "-" before that.
                if (i == 6) phone = phone + "-";
                // Add the digit to the phone charatcer string you are building.
                phone = phone + phoneNumeric.substr (i,1)
            }
        }
    }
    else
    { 
        // This means the tel no is in proper format. Make sure by replacing the 0th, 4th and 8th character.
        phone = "(" + phone.substring (1,4) + ") " + phone.substring (5,8) + "-" + phone.substring(9,13); 
    }
    // So far you are working internally. Refresh the screen with the re-formatted value.
    if (phone != telNo.value) telNo.value = phone;
}

/*
CheckTelNo method takes in current element of the form as input. This method should be 
fired as the user attempts to leave the current element in the form (by using onBlur method). 
It checks to see if the format of the phone is "(123) 456-7890".
Eg. <netui:textBox size="13" maxlength="13" onBlur="JavaScript:checkTelNo (this);" onKeyUp="JavaScript:formatTelNo (this);" onKeyDown="JavaScript:formatTelNo (this);"/>  
*/      
function checkTelNo (telNo)
{
    if (telNo.value == "") return;
    
    if (telNo.value.length >0 && telNo.value.length <12)
    {
		alert("Invalid phone number");
		telNo.select();
    }
    
    if (telNo.value.match (".[0-9]{3}.[0-9]{3}-[0-9]{4}") == null)
    {
        if (telNo.value.match ("[0-9]{10}") != null)
        {
            formatTelNo (telNo);
        }
    }
    
}

function validateUSDate(dateObj,finalCheck) 
{
/************************************************
DESCRIPTION: Validates that a string contains only
    valid dates with 2 digit month, 2 digit day,
    4 digit year. Date separator can be /
    Uses combination of regular expressions and
    string parsing to validate date.
    Ex. mm/dd/yyyy

PARAMETERS:
   dateObj - input object to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.


<input name="txtDate" onblur="return validateUSDate(form1.txtDate,true)" onkeyPress="validateUSDate(form1.txtDate)" maxlength="10" />

*************************************************/
 
 
  //var objRegExp = /^$|^[0-1]$|^[0-1][0-9][\/][0-3][0-9][\/][1-2][0-9][0-9][0-9]$/
 
	if(finalCheck) 
	{
		if(dateObj.value.length<10 && dateObj.value.length>0) dateObj.select();
	}
	
  var objRegExp = /^[0-1]?$|^[0-1][0-9]$|^[0-1][0-9][\/]$|^[0-1][0-9][\/][0-3]$|^[0-1][0-9][\/][0-3][0-9]$|^[0-1][0-9][\/][0-3][0-9][\/]$|^[0-1][0-9][\/][0-3][0-9][\/][1-2]$|^[0-1][0-9][\/][0-3][0-9][\/][1-2][0-9]$|^[0-1][0-9][\/][0-3][0-9][\/][1-2][0-9][0-9]$|^[0-1][0-9][\/][0-3][0-9][\/][1-2][0-9][0-9][0-9]$/


	var key=event.keyCode;
	var keyChar=String.fromCharCode(key);
	
	window.status=String.fromCharCode(keyChar)
	
	if(keyChar=='/')
	{
		window.status="/ canceled"
		event.returnValue=false;
		return false
	}	
	

	if( (dateObj.value.length==2 || dateObj.value.length==5) && key!=8) //backspace
	{
	 if(dateObj.value.length==2)
	 {
		if(parseInt(dateObj.value)<1 || parseInt(dateObj.value)>12)
		{
		 dateObj.value="";
		 event.returnValue=false;
		 return false;
		}
		
	 }
	 if(dateObj.value.length>4)
	 {
		if(parseInt(dateObj.value.substr(3,4))<1 || parseInt(dateObj.value.substr(3,4))>32)
		{
		 dateObj.value=dateObj.value.substr(0,3);
		 event.returnValue=false;
		 return false;
		}
		
	 }
	 dateObj.value=dateObj.value+'/'
	}
   	
	//check to see if in correct format
	if(!objRegExp.test(dateObj.value))
	{
		window.status=dateObj.value +":invalid:[" + dateObj.value.length +"]";
		dateObj.value=dateObj.value.substr(0,(dateObj.value.length-1))
		event.returnValue=false;
		return false; //doesn't match pattern, bad date
	}	
	else
	{
		window.status=dateObj.value+":ok["+dateObj.value.length+"]";
		return true;
	}
  
	return false;
}
/**********************/