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;
}

document.getElementsByClass=function(css, pa){
    pa= pa || document.body;
    var Rx, tem, A= [], O, who;
    Rx= typeof css== 'string'? RegExp('\\b'+css+'\\b'): Rx;
    if(!Rx || Rx.constructor!= RegExp) return A;
    O= pa.getElementsByTagName('*');
    for(var i= 0, L= O.length; i< L; i++){
        who= O[i];
        tem= who.getAttribute('class') || who.className;
        if(tem && Rx.test(tem)) A[A.length]= who;
    }
    return A;
}

function isZip(s) {
	// Check for correct zip code
	reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
	
	if (!reZip.test(s)) {
	  //alert("Zip Code Is Not Valid");
	  return false;
	}
	return true;
}
function submitForm(ref,refUrl,urlId) {
	if(ref=="") { ref = 34; }
        var zip = document.getElementById('ftZip').value;
	var type = document.getElementById('type').value;
        if(document.getElementsByClassName) {
            var zipcodes = document.getElementsByClassName('zipcode');
        } else {
            var zipcodes = document.getElementsByClass('zipcode');
        }
	switch(type) {
		case 'residential':
			if(zipcodes.length > 1) var zip = zipcodes[0].value;
		break;
		case 'commercial':
			if(zipcodes.length > 1) var zip = zipcodes[1].value;
		break;
	}
	if(type == "residential") {
//		var zip = document.getElementById('ftZip').value;	
		if (zip != "" && IsNumeric(zip) == true && isZip(zip) == true) {
			var link = "http://www.electricchoice.com/affiliateTable.php?ftZip=" + zip + "&type=" + type + "&ref=" + ref + "&refUrl=" + refUrl + "&urlId=" + urlId + "&tableType=1";
			$.fancybox({
			'href'				: link,
			'type'				: 'iframe',
        	'width'				: 800,
			'height'			: 700,
			'hideOnOverlayClick': false,
			'enableEscapeButton': false,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none'
			});
		} else {
			alert('Please enter a valid zip code.');
			if (!IsNumeric(zip)) alert('Zip code is not numeric');
			if (zip == "") alert('Zip code is empty');
			if (!isZip(zip)) alert('Not a zip code');
		}
	} else if (type == "commercial") {
		var zip = document.getElementById('ftZip').value;	
		if (zip == "Enter Your Zip Code" || zip == "") {
                    if(document.getElementsByClassName) {
			var zipcodes = document.getElementsByClassName('zipcode');
                    } else {
			var zipcodes = document.getElementsByClass('zipcode');                        
                    }
			if(zipcodes.length && zipcodes[1].value != "") {
				zip = zipcodes[1].value;
			} else {
				zip = prompt("Please enter your zip code","");
				var type = 'commercial';
			}
		}
		if (zip != "" && IsNumeric(zip) == true && isZip(zip) == true) {
			var link = "http://www.utilitychoice.com/commercial/orderForm/?ftZip=" + zip + "&ref=" + ref + "&refUrl=" + refUrl;
			$.fancybox({
			'href'				: link,
			'type'				: 'iframe',
        	'width'				: 750,
			'height'			: 700,
			'hideOnOverlayClick': false,
			'enableEscapeButton': false,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none'
			});
		} else {
/*                    if(zip == "") alert('Please enter a ' + type + ' zip code');
                    if(!IsNumeric(zip)) alert('Please enter a numeric ' + type + ' zip code');
                    if(!isZip(zip)) alert('Please enter a valid ' + type + ' zip code');*/
				zip = prompt("Please enter your zip code","");
//			alert('Please enter a valid zip code.');
		}
	} else {
		alert('Please select a type -- Residential or Commercial.');
	}
}
function checkEnter(e,fm,ref){ //e is event object passed from function invocation
	var characterCode //literal character code will be stored in this variable
	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	} else{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}
	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		submitForm(fm,ref);
		return false 
	} else{
		return true 
	}

}
