var timeout         = 500;
var closetimer		= 0;
var ddmenuitem      = 0;

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
//var phoneNumberDelimiters = "()- ";
var phoneNumberDelimiters = "";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
	var bracket=3
	strPhone=trim(strPhone)
	//if(strPhone.indexOf("+")>1) return false
	//if(strPhone.indexOf("-")!=-1)bracket=bracket+1
	//if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
	//var brchr=strPhone.indexOf("(")
	//if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
	//if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length == minDigitsInIPhoneNumber);
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}
	
function validate_form ( )
{
	valid = true;
	var emailID=document.quote_part_one.email;
	var alert_text = "";
	var Phone=document.quote_part_one.home_phone;
	var Phone2=document.quote_part_one.work_phone;
        var fax=document.quote_part_one.fax;
	
        if ( document.quote_part_one.first_name.value == "" )
        {
                alert_text = alert_text + "Please fill in your First Name.\n";
				valid = false;
        }
        
		if ( document.quote_part_one.last_name.value == "" )
        {
                alert_text = alert_text + "Please fill in your Last Name.\n" ;
                valid = false;
        }
		
		/*
	        if ( document.quote_part_one.email.value == "" )
	        {
	                alert_text = alert_text + "Please fill in your e-mail address.\n" ;
	                valid = false;
	        }*/

                /*
		if ((emailID.value==null)||(emailID.value=="")){
                alert_text = alert_text + "Please fill in your e-mail address.\n" ;
                valid = false;
				emailID.focus();
		}
                */
		
		if (echeck(emailID.value)==false){
                alert_text = alert_text + "Please fill in your e-mail address correctly.\n" ;
                valid = false;
				emailID.focus();
		}	
		
		
        /*
		if ( document.quote_part_one.home_phone.value == "" && document.quote_part_one.work_phone.value == "" )
		        {
		                alert_text = alert_text + "Please fill in your home phone or work phone.\n" ;
		                valid = false;
		 } */
		
		/*if ((Phone.value==null)||(Phone.value=="")){
			alert_text = alert_text + "Please fill in your home phone.\n" ;
			valid = false;
			Phone.focus();
		}
		*/

		if (checkInternationalPhone(Phone.value)==false){
			alert_text = alert_text + "Please fill in your home phone correctly.\n" ;
			valid = false;
			Phone.value="";
			Phone.focus();
		}		

                /*
		if ((Phone2.value==null)||(Phone2.value=="")){
			alert_text = alert_text + "Please fill in your work phone.\n" ;
			valid = false;
			Phone2.focus();
		}
		*/

		if (checkInternationalPhone(Phone2.value)==false){
			alert_text = alert_text + "Please fill in your work phone correctly.\n" ;
			valid = false;
			Phone2.value="";
			Phone2.focus();
		}

                var faxstr = fax.value;
		if ( faxstr.length>0 ) {
                    if (checkInternationalPhone(fax.value)==false){
                            alert_text = alert_text + "Please fill in your fax phone correctly.\n" ;
                            valid = false;
                            fax.value="";
                            fax.focus();
                    }
		}


		
        if ( document.quote_part_one.from_city.value == "" )
        {
                alert_text = alert_text + "Please fill in the city the you are moving from.\n" ;
                valid = false;
        }
        if ( document.quote_part_one.from_state.selectedIndex == 0 )
        {
                alert_text = alert_text + "Please select in the state that you are moving from.\n" ;
                valid = false;
        }
        if ( document.quote_part_one.to_city.value == "" )
        {
                alert_text = alert_text + "Please fill in the city that you are moving to.\n" ;
                valid = false;
        }
        if ( document.quote_part_one.to_state.selectedIndex == 0 )
        {
                alert_text = alert_text + "Please select in the state that you are moving to" ;
                valid = false;
        }
	if (alert_text != "")
	{
		alert (alert_text);
	}
    return valid;
    }

    function validatePhone(phoneField, format, phone) {

        valid = true;

        var num = phoneField.value.replace(/[^\d]/g,'');

        if(num.length == 0 ) {
           valid = true;
        } else if(num.length != 10 && num.length != 0 ) {
            //Alert the user that the phone number entered was invalid.
            alert('Please enter a valid phone number including area code in '+ phone);
            valid = false;
        } else {
            //Email was valid.  If format type is set, format the Phone to the desired style.
           switch(format) {
                case '0': //Format (xxx)xxx-xxxx
                    phoneField.value = "(" + num.substring(0,3) + ")" + num.substring(3, 6) + "-" + num.substring(6);
                    break;
                case '1': //Format xxx-xxx-xxxx
                    phoneField.value = num.substring(0,3) + "-" + num.substring(3, 6) + "-" + num.substring(6);
                    break;
                default: //Format xxxxxxxxxx
                    phoneField.value = num;
                    break;
            }
        }
        return true;
    }

function submitForm(pagename){
        document.estimate.action = pagename;
        document.estimate.submit();
}

function navigation_open()
{	navigation_canceltimer();
	navigation_close();
	ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');}

function navigation_close()
{	if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function navigation_timer()
{	closetimer = window.setTimeout(navigation_close, timeout);}

function navigation_canceltimer()
{	if(closetimer)
	{	window.clearTimeout(closetimer);
		closetimer = null;}}

$(document).ready(function()
{	$('#navigation > li').bind('mouseover', navigation_open);
	$('#navigation > li').bind('mouseout',  navigation_timer);});

document.onclick = navigation_close;
