// JavaScript Document


function validateForm() {
  var submit = true;
  var statecode = document.getElementById('statecode').value;
  //var sid = document.getElementById('sid').value;
  if(statecode == '') { submit = false;}
  if( submit) {
   // window.location = "/listing/index/statecode/" + statecode+"/sid/"+sid;
   window.location = "/listing/index/statecode/" + statecode;
  }
  else{
	alert('Please select your state.');
  }
}

function validateForm2() {
    var statecode = document.getElementById('statecode') != null ? document.getElementById('statecode').value : "";
    var zip_code = document.getElementById('zipcode') != null ? document.getElementById('zipcode').value : "";
    var url = "";
    var useCS = true;
    var thisWindow = ""; 
    var th = document.getElementById('th').value;

    if (zip_code != "") {  // zip submit page
	thisWindow = "/listing/index/zip/" + zip_code;
    } 
    else { 
	if( (th == "rc4" || th == "rc5") && zip_code == '') {
	    alert('Please enter your 5 digit zip code');
	    return;
	}
	else if( (th == "lp4" || th == "lp5") && statecode == '') {
	    alert('Please select your state.');
	    return;
	}
    }

    exitp = false;

    if (document.getElementById('sec') != null && document.getElementById('sec').value == 1) 
	useCS = false;

    if (useCS) {
	var usecase = document.getElementById('usecase').value;
	switch (usecase) {
	case '1':	 
            url = "http://www.carsandinsurance.com/cs/auto-form.php?dest=listing";
            break;
	case '2':
            url = "http://www.carsandinsurance.com/cs/auto-form.php?dest=seqr&l=1";
            break;
	case '3':
            url ="http://www.carsandinsurance.com/cs/auto-form.php?dest=seqf";
            break;
	case '4':
            url = "http://www.carsandinsurance.com/cs/auto-form.php";
            break;
	}
	if ( zip_code && zip_code.length === 5)
	    url += "&contZip=" + zip_code.value;
    }

    var sid = document.getElementById('sid').value;
    if (th == "rc4" || th== "lp4") {
//	url = "http://www.carsandinsurance.com/qw/" + th + "/index.html?sid=" + sid + "&state=" + statecode + "&zip=" + zip_code;
    }

   //alert("url is "+url);
   if (url != "") {
       window.open(url, "CAI", 'width=1050,toolbar=1,scrollbars=yes,resizable=yes');
       window.parent.focus();
   }

   var pixString = '';
   if( document.getElementById('pix') && "rc2" == document.getElementById('pix').value) {
       pixString = "/pix/rc2";
   }

   if(thisWindow == "") thisWindow = "/listing/index/statecode/" + statecode + pixString;
   if (document.getElementById('form1')) 
     document.getElementById('form1').action = thisWindow;
    else
     window.location = thisWindow;
   return true;
}

function getXML_HTTPObj() {
    var xmlHTTPObj;

    try {
        // Firefox, Opera 8.0+, Safari
        xmlHTTPObj = new XMLHttpRequest();
        return xmlHTTPObj;
    }
    catch(e) {
        // Internet Explorer
        try {
            xmlHTTPObj = new ActiveXObject("Msxml2.XMLHTTP");
            return xmlHTTPObj;
        }
        catch(e) {
            try {
                xmlHTTPObj = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e) {
                alert("Your browser does not support AJAX!");
                return;// false;
            }
        }
    }
}

//function find_state(obj) {
//  if (obj.value.length == 5) {
//      xmlhttp = getXML_HTTPObj();
//      xmlhttp.open("GET", "/zip.php?q="+obj.value, false);
//      xmlhttp.send();
//      return xmlhttp.responseText;
//  }
//  else
//      return "";
//}


        function isValidZip(obj) {
            if (checkEmpty(obj, '') == false || !isNumeric(obj.value) || obj.value.length != 5 || obj.value == '00000') {
                //obj.focus();
                return false;
            } else
                return true;

        }

        function isValidZipDb(obj, result) { // database check on zip

            var stt = "";
            if(obj.value.length == 5) {
                var option = "&out=";
                if( result == "st") {
		    option += "st";
                }
                else {
		    option += "zip";
	    	}

		xmlhttp = getXML_HTTPObj();
		xmlhttp.open("GET", "/zip.php?q="+obj.value+option, false);
      		xmlhttp.send();
		stt = xmlhttp.responseText;
	    }

            if ( stt.length == "") { 
		return false;
	    }
	    else if(result == "st") { 
              document.getElementById('statecode').value = stt;
            }
      return true;
        }

        function checkEmpty(obj, val) {
            var len = obj.length;

            //alert(obj.value+" - "+obj.value.length);
            if (len == 0) {
                //alert(val +" cannot be blank");
                return false;
            } else {
                return true;
            }
        }
        function isNumeric(s) {
            if (s == '') return true;
            if (!/^-*[0-9\.]+$/.test(s)) {
                return false
            } else {
                return true;
            }
        }


