function BuildProtocolList(iOpt)
{
	//alert(document.forms[0].dpbx_protocol.selectedIndex);
	if(document.forms[0].dpbx_protocol.selectedIndex == 2)	
	{
		document.forms[0].dpbx_codec.options[0] = null;
		//alert("here");
	}else{
		document.forms[0].dpbx_codec.options.length = 0;
		document.forms[0].dpbx_codec.options[0] = new Option('G729','g729r8');
		document.forms[0].dpbx_codec.options[1] = new Option('G711uLAW','g711ulaw');
	}
	
}

function checkForm(pageNumber)
{
	switch (pageNumber) 
	{
	   case "1" : 
		   	if (document.forms[0].dpbx_numberOfLines.options[document.forms[0].dpbx_numberOfLines.selectedIndex].value !="0")
			{
				document.forms[0].dpbx_state.disabled = false;
			}
		    break; 
	   case "2" : 
	      
		      break; 
	   case "3" : 
	      
		      break; 
	   case "4" : 
	      
		      break; 
	   default : 
	 } 
}

function sumbitOrderInfoForm(arrayItem)
{
	//alert("We want to delete this guy: " + arrayItem);
	document.forms['myOrderForm'].hdn_deleteArrayNum.value = arrayItem;
	document.forms['myOrderForm'].submit();	
}

function validNumLines()
{
	if (document.forms[0].dpbx_numberOfLines.options[document.forms[0].dpbx_numberOfLines.selectedIndex].value !="0")
	{
		document.forms[0].dpbx_state.disabled = false;
		document.forms[0].submit();
	}else{
		document.forms[0].dpbx_state.selectedIndex = 0;
		document.forms[0].dpbx_state.disabled = true;
		document.forms[0].dpbx_city.selectedIndex = 0;
		document.forms[0].dpbx_city.disabled = true;
		document.forms[0].dpbx_phoneBlocks.disabled = true;
	}
}

function updateOutboundCallMsg()
{
	if(document.forms[0].chk_OutGoingCalls.checked==true)
	{
		//alert(document.forms[0].chk_OutGoingCalls.checked);
		document.forms[0].hdn_outGoingCalls.value = "Yes";
		//alert(document.forms[0].hdn_outGoingCalls.value);
	}else{
		//alert(document.forms[0].chk_OutGoingCalls.checked);
		document.forms[0].hdn_outGoingCalls.value = "No";
	}
	//Check Valid Number of lines
	if (document.forms[0].dpbx_numberOfLines.options[document.forms[0].dpbx_numberOfLines.selectedIndex].value !="0")
	{
		document.forms[0].dpbx_state.disabled = false;
	}else{
		document.forms[0].dpbx_state.selectedIndex = 0;
		document.forms[0].dpbx_state.disabled = true;
		document.forms[0].dpbx_city.selectedIndex = 0;
		document.forms[0].dpbx_city.disabled = true;
		document.forms[0].dpbx_phoneBlocks.disabled = true;
	}
	//Check state
	if (document.forms[0].dpbx_state.options[document.forms[0].dpbx_state.selectedIndex].value !="Select State")
	{
		//document.forms[0].dpbx_city.disabled = false;
		document.forms[0].hdn_fieldChange.value = "state";
	}else{
		document.forms[0].dpbx_city.selectedIndex = 0;
		document.forms[0].dpbx_city.disabled = true;
		document.forms[0].dpbx_phoneBlocks.disabled = true;
	}
	document.forms[0].submit();
}

function updateSIPREG()
{
	if(document.forms[0].chk_SIPREG.checked==true)
	{
		//alert(document.forms[0].chk_OutGoingCalls.checked);
		document.forms[0].hdn_SIPREG.value = "Yes";
		document.forms[0].dpbx_state.selectedIndex = 0;
		document.forms[0].dpbx_state.disabled = true;
		document.forms[0].dpbx_city.selectedIndex = 0;
		document.forms[0].dpbx_city.disabled = true;
		document.forms[0].dpbx_phoneBlocks.disabled = false;
		document.forms[0].dpbx_phoneBlocks.selectedIndex = 0;
		document.forms[0].dpbx_phoneBlocks.disabled = true;
		//alert(document.forms[0].hdn_outGoingCalls.value);
	}else{
		//alert(document.forms[0].chk_OutGoingCalls.checked);
		document.forms[0].hdn_SIPREG.value = "No";
		document.forms[0].dpbx_state.selectedIndex = 0;
		document.forms[0].dpbx_state.disabled = true;
		document.forms[0].dpbx_city.selectedIndex = 0;
		document.forms[0].dpbx_city.disabled = true;
		document.forms[0].dpbx_phoneBlocks.disabled = false;
		document.forms[0].dpbx_phoneBlocks.selectedIndex = 0;
		document.forms[0].dpbx_phoneBlocks.disabled = true;
	}
	document.forms[0].submit();
}

function checkReferralSelection()
{
	if(document.forms[0].dpbx_referralAgent.options[document.forms[0].dpbx_referralAgent.selectedIndex].value !="Other")
	{
		//alert(document.forms[0].dpbx_referralAgent.options[document.forms[0].dpbx_referralAgent.selectedIndex].value);
		document.forms[0].txt_referralOther.value="";	
		document.forms[0].txt_referralOther.disabled=true;	
	}else{
		//alert(document.forms[0].dpbx_referralAgent.options[document.forms[0].dpbx_referralAgent.selectedIndex].value);
		document.forms[0].txt_referralOther.value="";	
		document.forms[0].txt_referralOther.disabled=false;	
	}
	
}

function StateFieldChange()
{
	if (document.forms[0].dpbx_state.options[document.forms[0].dpbx_state.selectedIndex].value !="Select State")
	{
		//document.forms[0].dpbx_city.disabled = false;
		document.forms[0].hdn_StateValue.value = document.forms[0].dpbx_state.options[document.forms[0].dpbx_state.selectedIndex].value;
		document.forms[0].hdn_fieldChange.value = "state";
		document.forms[0].submit();
	}else{
		document.forms[0].dpbx_city.selectedIndex = 0;
		document.forms[0].dpbx_city.disabled = true;
		document.forms[0].dpbx_phoneBlocks.disabled = true;
	}
}

function CityFieldChange()
{
	if (document.forms[0].dpbx_city.options[document.forms[0].dpbx_city.selectedIndex].value !="Select City")
	{
		//document.forms[0].dpbx_areaCode.disabled = false;
		document.forms[0].hdn_fieldChange.value = "city";
		document.forms[0].submit();
	}else{
		document.forms[0].dpbx_city.selectedIndex = 0;
		document.forms[0].dpbx_city.disabled = true;
		document.forms[0].dpbx_phoneBlocks.disabled = true;
	}
}

function AreaCodeFieldChange()
{
	if (document.forms[0].dpbx_AreaCode.options[document.forms[0].dpbx_AreaCode.selectedIndex].value !="Select Area Code")
	{
		//document.forms[0].dpbx_areaCode.disabled = false;
		document.forms[0].hdn_fieldChange.value = "areaCode";
		document.forms[0].submit();
	}else{
		document.forms[0].dpbx_city.selectedIndex = 0;
		document.forms[0].dpbx_city.disabled = true;
		document.forms[0].dpbx_phoneBlocks.disabled = true;
	}
}

function validStateSelection()
{
	if (document.forms[0].dpbx_state.options[document.forms[0].dpbx_state.selectedIndex].value !="Select State")
	{
		document.forms[0].dpbx_city.disabled = false;
		getLocations(document.forms[0].dpbx_state.options[document.forms[0].dpbx_state.selectedIndex].value);
	}else{
		document.forms[0].dpbx_city.selectedIndex = 0;
		document.forms[0].dpbx_city.disabled = true;
		document.forms[0].dpbx_phoneBlocks.disabled = true;
	}
}

function validCitySelection()
{
	if (document.forms[0].dpbx_city.options[document.forms[0].dpbx_city.selectedIndex].value !="Select City")
	{
		document.forms[0].dpbx_phoneBlocks.disabled = false;
		document.forms[0].dpbx_phoneBlocks.selectedIndex = 1;
	}else{
		document.forms[0].dpbx_city.selectedIndex = 0;
		document.forms[0].dpbx_phoneBlocks.disabled = true;
	}
}

function ValidateMLUOrderForm()
{
	var allowSubmit = true;
	if(document.forms[0].dpbx_city.selectedIndex == 0)
	{
		allowSubmit = false;
	}
	if(document.forms[0].dpbx_phoneBlocks.selectedIndex == 0)
	{
		allowSubmit = false;
	}
	if(document.forms[0].dpbx_numberOfLines.selectedIndex == 0)
	{
		allowSubmit = false;
	}
	if (allowSubmit == false)
	{
		document.forms[0].hdn_pageRefNum.value = "0";
		//alert("page ref: " + document.forms[0].hdn_pageRefNum.value);
	}
}
function setSubmitValue(btnSelected)
{
	if(btnSelected != "")
	{
		document.forms[0].hdn_submitValue.value = btnSelected;
		if(btnSelected == "Previous")
		{
			window.history.go(-1);
		}
	}
	
}

function ValidateProvisioningForm()
{
	//See if button selected was the previous button
	//to restrict forward movement.
	var btn_submitText = "";
	//btn_submitText = document.forms[0].btn_Continue.value;
	btn_submitText = document.forms[0].hdn_submitValue.value;
	//alert(btn_submitText);
	if (btn_submitText == "Continue")
	{
		var ipAddress = "";
		var domainName = "";
		var bValidIP = false;
		ipAddress = document.forms[0].txt_IP1.value + "." + document.forms[0].txt_IP2.value + "." + document.forms[0].txt_IP3.value + "." + document.forms[0].txt_IP4.value;
		domainName = document.forms[0].txt_provDomain.value;
		//alert("the ip: " + ipAddress);
		//bValidIP = verifyIP(ipAddress);
		if (isBlank(domainName))
		{
			bValidIP = isValidIPAddress(ipAddress)
			if(bValidIP)
			{
				document.forms[0].hdn_IPAddress.value = ipAddress;
				return true;
			}else{
				alert(ipAddress + " is not a valid IP address.\n Please enter a valid IP Address or Domain");
				return false;
			}
		}else{
			if(checkDomain(domainName)){
					return true;
				}else{
					return false;
				}
		}	
	}
}

/*function isValidIPAddress(ipaddr) 
{
   var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
   if (re.test(ipaddr)) 
   {
      var parts = ipaddr.split(".");
      if (parseInt(parseFloat(parts[0])) == 0) { return false; }
      for (var i=0; i<parts.length; i++) {
         if (parseInt(parseFloat(parts[i])) > 255) { return false; }
      }
      return true;
   } else {
      return false;
   }
}
*/
var validChars = '.0123456789';

function isValidIPAddress(what) {
    if (!what)
        return false;

    dots = 0;

    for (var i = 0; i < what.length; i++) {
       var chr = what.substring(i,i+1);
       if (validChars.indexOf(chr) == -1)
           return false;
       if (chr == '.') {
           dots++;
           eval('dot' + dots + ' = ' + i);
       }
    }

    if (dots != 3)
        return false;
    
    if (what.substring(0,1) == '.' || what.substring(what.length,what.length+1) == '.')
        return false;

    ip1 = what.substring(0,dot1);
    if (!ip1 || ip1 >255 || ip1==10 || ip1==172 || ip1==192 || ip1==169)
        return false;
    ip2 = what.substring(dot1+1,dot2);
    if (!ip2 || ip2 >255)
        return false;
    ip3 = what.substring(dot2+1,dot3);
    if (!ip3 || ip3 >255)
        return false;
    ip4 = what.substring(dot3+1,what.length+1);
    if (!ip4 || ip4 >255)
        return false;

    if (ip1 == 0 && ip2 == 0 && ip3 == 0 && ip4 == 0)
        return false;

    return true;
}


function ValidatePaymentInfoForm()
{
	var btn_submitText = "";
	//btn_submitText = document.forms[0].btn_Continue.value;
	btn_submitText = document.forms[0].hdn_submitValue.value;
	//alert(btn_submitText);
	if (btn_submitText == "Continue")
	{
		//Check First name
		var firstName = "";	
		firstName = document.forms[0].txt_firstName.value;
		var bFirstName = checkName(firstName);
		if(bFirstName == false)
		{
			alert("Please enter a valid first name.");
			return false;
		}
		//Check last name
		var lastName = "";
		lastName = document.forms[0].txt_lastName.value;
		var bLastName = checkName(lastName);	
		if(bLastName == false)
		{
			alert("Please enter a valid last name.");
			return false;
		}
		//Check for blank address1 field
		var streetAddress = "";
		streetAddress = document.forms[0].txt_streetAddress1.value;	
		var isAddressBlank = isBlank(streetAddress);
		if(isAddressBlank)
		{
			alert("Please enter a street address.");
			return false;
		}
		//Check for blank city field
		var city = "";
		city = document.forms[0].txt_city.value;	
		var isCityBlank = isBlank(city);
		if(isCityBlank)
		{
			alert("Please enter a city.");
			return false;
		}
		//Check for blank state/province field
		var province = "";
		province = document.forms[0].txt_state.value;	
		var isProvinceBlank = isBlank(province);
		if(isProvinceBlank)
		{
			alert("Please enter a state/province.");
			return false;
		}
		//Check for blank zip field
		var zip = "";
		zip = document.forms[0].txt_postalCode.value;	
		var isZipBlank = isBlank(zip);
		if(isZipBlank)
		{
			alert("Please enter a postal code.");
			return false;
		}
		//Check for blank country field
		var country = "";
		country = document.forms[0].txt_country.value;	
		var isCountryBlank = isBlank(country);
		if(isCountryBlank)
		{
			alert("Please enter a country.");
			return false;
		}
		//Check for blank phone number field
		var phonenum = "";
		phonenum = document.forms[0].txt_phone.value;	
		var isPhonenumBlank = isBlank(phonenum);
		if(isPhonenumBlank)
		{
			alert("Please enter a phone number.");
			return false;
		}
		//Check for email
		var email = "";
		email = document.forms[0].txt_email1.value;	
		var isEmailValid = checkEmail(email);
		if(!isEmailValid)
		{
			alert("Please enter a valid email address.");
			return false;
		}
		//Confirm email address
		var confirmEmail = "";
		confirmEmail = document.forms[0].txt_email2.value;
		if(email != confirmEmail)
		{
			alert("The email addresses entered do not match.");
			return false;
		}
		//Basic Credit Card Check
		var ccardNum = "";
		ccardNum = document.forms[0].txt_creditCardNumber.value
		if(isBlank(ccardNum))
		{
			alert("Please enter a credit card number.");
			return false;
		}
		if((ccardNum.length < 14) || (ccardNum.length > 16))
		{
			alert("Enter a valid credit card number.");
			return false;
		}
		if(!IsNumeric(ccardNum))
		{ 
			alert("Enter a valid credit card number.");
			return false;
		}
		//Check csc number
		var cscNum = "";
		cscNum = document.forms[0].txt_csc.value
		if(isBlank(cscNum))
		{
			alert("Please enter a csc number.");
			return false;
		}
		//Check Referred To us
		var refToUs = "";
		var refToUsOther="";
		refToUs = document.forms[0].dpbx_referralAgent.selectedIndex
		refToUsOther = document.forms[0].txt_referralOther.value
		if(refToUs==0)
		{			
			alert("Please enter how you were referred to us?");
			return false;
		}
		if(refToUs==5)
		{
			if (isBlank(refToUsOther))
			{
				alert("Since you chose Other, Please enter the source?");
				return false;
			}
		}
		if (!document.forms[0].chk_termsConditions.checked) 
		{
			alert("You must agree to the terms to complete order.");
			return false;
		} 
		return true;
	}
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         	IsNumber = false;
         }
      }
   return IsNumber;
}


function checkEmail(emailAddress) 
{
	//alert("Checking email address");
	var expr = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
	if (expr.test(emailAddress))
	{
		return true;
	}
	return false;
}
function isBlank(text)
{
	var sText = text.replace(/[\s]+$/g,"");
	if(sText == "") 
	{
		return true;
	}else{
		return false;
	}
	
}

function checkName(custName)
{
	var nameString = "";
	nameString = custName.replace(/[\s]+$/g,"");
	if(nameString == "") { return false;}
	var onlyChrsGrt1 = /[a-zA-Z]/;
   if (onlyChrsGrt1.test(custName)) 
	{
     	return true;
   }else{
		return false;
	}
	alert("name check");
}

function autotab(original,destination)
{
	if (original.getAttribute&&original.value.length==original.getAttribute("maxlength"))
	destination.focus()
}
function checkDomain(nname)
{
var arr = new Array(
'.com','.net','.org','.biz','.coop','.info','.museum','.name',
'.pro','.edu','.gov','.int','.mil','.ac','.ad','.ae','.af','.ag',
'.ai','.al','.am','.an','.ao','.aq','.ar','.as','.at','.au','.aw',
'.az','.ba','.bb','.bd','.be','.bf','.bg','.bh','.bi','.bj','.bm',
'.bn','.bo','.br','.bs','.bt','.bv','.bw','.by','.bz','.ca','.cc',
'.cd','.cf','.cg','.ch','.ci','.ck','.cl','.cm','.cn','.co','.cr',
'.cu','.cv','.cx','.cy','.cz','.de','.dj','.dk','.dm','.do','.dz',
'.ec','.ee','.eg','.eh','.er','.es','.et','.fi','.fj','.fk','.fm',
'.fo','.fr','.ga','.gd','.ge','.gf','.gg','.gh','.gi','.gl','.gm',
'.gn','.gp','.gq','.gr','.gs','.gt','.gu','.gv','.gy','.hk','.hm',
'.hn','.hr','.ht','.hu','.id','.ie','.il','.im','.in','.io','.iq',
'.ir','.is','.it','.je','.jm','.jo','.jp','.ke','.kg','.kh','.ki',
'.km','.kn','.kp','.kr','.kw','.ky','.kz','.la','.lb','.lc','.li',
'.lk','.lr','.ls','.lt','.lu','.lv','.ly','.ma','.mc','.md','.mg',
'.mh','.mk','.ml','.mm','.mn','.mo','.mp','.mq','.mr','.ms','.mt',
'.mu','.mv','.mw','.mx','.my','.mz','.na','.nc','.ne','.nf','.ng',
'.ni','.nl','.no','.np','.nr','.nu','.nz','.om','.pa','.pe','.pf',
'.pg','.ph','.pk','.pl','.pm','.pn','.pr','.ps','.pt','.pw','.py',
'.qa','.re','.ro','.rw','.ru','.sa','.sb','.sc','.sd','.se','.sg',
'.sh','.si','.sj','.sk','.sl','.sm','.sn','.so','.sr','.st','.sv',
'.sy','.sz','.tc','.td','.tf','.tg','.th','.tj','.tk','.tm','.tn',
'.to','.tp','.tr','.tt','.tv','.tw','.tz','.ua','.ug','.uk','.um',
'.us','.uy','.uz','.va','.vc','.ve','.vg','.vi','.vn','.vu','.ws',
'.wf','.ye','.yt','.yu','.za','.zm','.zw');

var mai = nname;
var val = true;

var dot = mai.lastIndexOf(".");
var dname = mai.substring(0,dot);
var ext = mai.substring(dot,mai.length);
//alert(ext);
	
if(dot>2 && dot<57)
{
	for(var i=0; i<arr.length; i++)
	{
	  if(ext == arr[i])
	  {
	 	val = true;
		break;
	  }	
	  else
	  {
	 	val = false;
	  }
	}
	if(val == false)
	{
	  	 alert("Your domain extension "+ext+" is not correct\n Please use format - www.mydomain.com");
		 return false;
	}
	else
	{
		for(var j=0; j<dname.length; j++)
		{
		  var dh = dname.charAt(j);
		  var hh = dh.charCodeAt(0);
		  if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || hh==45 || hh==46)
		  {
			 if((j==0 || j==dname.length-1) && hh == 45)	
		  	 {
		 	  	 alert("Domain name should not begin are end with '-'\n Please use format - www.mydomain.com");
			      return false;
		 	 }
		  }
		else	{
		  	 alert("Your domain name should not have special characters\n Please use format - www.mydomain.com");
			 return false;
		  }
		}
	}
}
else
{
 alert("Your Domain name is too short/long.\n Please use format - www.mydomain.com");
 return false;
}	

return true;
}