﻿JSONscriptRequest.scriptCounter = 1;

function JSONscriptRequest(fullUrl) {
    this.fullUrl = fullUrl;
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    this.headLoc = document.getElementsByTagName("head").item(0);
    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}

JSONscriptRequest.prototype.buildScriptTag = function () {
    this.scriptObj = document.createElement("script");
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
JSONscriptRequest.prototype.removeScriptTag = function () {
    this.headLoc.removeChild(this.scriptObj); 
}

JSONscriptRequest.prototype.addScriptTag = function () {
    this.headLoc.appendChild(this.scriptObj);
}

function isZip(strValue) 
{
    var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;

    //check for valid US Zipcode
    return objRegExp.test(strValue);

}

function bwformValidation(form) {
    form = document.getElementById('bwsbgform_'+form)
    formID = form.bw_formId.value;
    Location = form.bw_Location.value;
    boxType = form.bw_BoxType.value;
    Source = form.bw_Source.value;
    
    if (!isZip(Location)) {
        alert("Please enter a valid Zip Code");
        return false;
    }
    prodID = "";
    if (form.bw_ProdId.tagName == "SELECT" || form.bw_ProdId.tagName == "INPUT") {
        prodID = form.bw_ProdId.value;
    } else {
        for (i=0;i<form.bw_ProdId.length;i++) {
	        if (form.bw_ProdId[i].checked) {
		        prodID = form.bw_ProdId[i].value;
	        }
        }
    }
   
    if (prodID == "") {
        alert("Please select a health insurance type");
        return false;
    }
    
    var obj=new JSONscriptRequest('http://www.brokersweb.com/SearchBoxLandingRule.aspx?FormId=' + formID + '&Location=' + Location + '&ProdId=' + prodID + '&BoxType=' + boxType + '&Source=' + Source);
    obj.buildScriptTag(); // Build the script tag     
    obj.addScriptTag(); // Execute (add) the script tag
    return false;
}

function landing(data)
{
  var text='';
  if(data==null)
	alert('error');
  else
  {
    if (data.id > 0)
        window.location = data.url;
    else
        alert("There was an error processing your request please try again later.");
  }
}