﻿// JScript File
function copyCorp(){
    // copies corporate information down
    copyCorpSub("Name");
    copyCorpSub("Address");
    copyCorpSub("City");
    copyCorpSub("State");
    copyCorpSub("Zip");
    copyCorpSub("Phone");
    copyCorpSub("Fax");
    copyCorpSub("Contact");
    return false;
}
function copyCorpSub(fieldName){
    var oS=document.getElementById("Business_"+fieldName);
    var oD=document.getElementById("Corporate_"+fieldName);
    oD.value=oS.value;
}




// VALIDATION

function validateForm(sContainer){
    var oContainer=document.getElementById(sContainer);
    if(!oContainer){alert("Bad object");return false;}
    var bValid=true;
    var oF,cF=oContainer.getElementsByTagName("input");
    // APPEND SELECT BOXES SOMEHOW!!!
    var cRad,bFoundOne;
    var ValidationMessage = new Array();
    var cRequired,cValue;
    var oMsgSpan,iTop=0,iLeft=0,iWidth=0,oBM;
    for(x=cF.length-1;x>=0;x--){
        oF=cF[x];
        while(ValidationMessage.length>0){ValidationMessage.pop();}
        cRequired=checkClass(oF,"v_required");
        switch(oF.type){
            case "text":
                cValue=oF.value;
                if(cRequired&&cValue==""){ValidationMessage.push("Required");}
                if(checkClass(oF,"v_date")&&!valDate(cValue)){ValidationMessage.push("mm/dd/yyyy");}
                if(checkClass(oF,"v_state")&&!valStateCode(cValue)){ValidationMessage.push("Must be a valid state");}
                if(checkClass(oF,"v_email")&&!valEmail(cValue)){ValidationMessage.push("Must be a valid email");}
                if(checkClass(oF,"v_zipcode")&&!valZip(cValue)){ValidationMessage.push("Must be a valid zip code");}
               break;
            case "select-one":
                if(cRequired&&oF.selectedIndex==0){ValidationMessage.push("Required");}
                break;
            case "radio":
                if(cRequired){
                    // get all radios with this name
                    // make sure at least one is checked
                    bFoundOne=false;
                    cRad=document.getElementsByName(oF.name);
                    for(y=0;y<cRad.length;y++){if(cRad[y].checked){bFoundOne=true;}}
                    if(!bFoundOne){ValidationMessage.push("Required");}
                }
                break;
        }// switch
        oMsgSpan=document.getElementById(oF.id+"_msg");
        if(oMsgSpan){oMsgSpan.innerHTML=""};
        if(ValidationMessage.length>0){
            bValid=false;
            if(!oMsgSpan){
                oMsgSpan=document.createElement("span");
                oMsgSpan.id=oF.id+"_msg";
                oMsgSpan.className="err";
                if(document.getBoxObjectFor){
                    var oBM = document.getBoxObjectFor(oF); 
                    iTop=oBM.y;
                    iLeft=oBM.x;
                    iWidth=oBM.width;
                }
                else if (oF.getBoundingClientRect){
                    oBM=oF.getBoundingClientRect();
                    iTop=oBM.top+document.body.scrollTop;
                    iLeft=oBM.left;
                    iWidth=oBM.right-oBM.left;
                }
                if(oF.type=="radio"){iLeft+=22;}
                oMsgSpan.style.top=iTop+4;
                oMsgSpan.style.left=iLeft+iWidth+5;
                oContainer.appendChild(oMsgSpan);
             }
            oMsgSpan.innerHTML=ValidationMessage.join(" ");
            //oF.onchange=eval("function(){clearMsg('" + oMsgSpan.id + "')}");
            oMsgSpan=null;
        }
    }// next
    if(!bValid){alert("Please ensure that all required fields are completed properly.");}
    return bValid;
}
function checkClass(obj,className){
    return obj.className.indexOf(className)!=-1;
}
function clearMsg(msgID){
    document.getElementById(msgID).innerHTML="";
}

function valNum(){
	var iK = event.keyCode;
	if(iK<48||iK>57){
		event.returnValue=null;
	}
}
function valStateCode(sSC){
	var sStates=",AL,AK,AZ,AR,CA,CO,CT,DE,DC,FL,GA,HI,ID,IL,IN,IA,KS,KY,LA,ME,MD,MA,MI,MN,MS,MO,MT,NE,NV,NH,NJ,NM,NY,NC,ND,OH,OK,OR,PA,RI,SC,SD,TN,TX,UT,VT,VA,WA,WV,WI,WY,"
	var sSearch=","+sSC.toUpperCase()+",";
	if(sStates.indexOf(sSearch)==-1&&sSearch!=",,"){
		return false;
	}
	else{
		return true;
	}
}
function valZip(val){
	var oReg=new RegExp(/^((\d{5}-\d{4})|(\d{5})|([AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\d[A-Za-z]\s?\d[A-Za-z]\d))$/);
	if(oReg.test(val)||val==""){
		return true;
		}
	else{
		return false;
	}
}
function valEmail(val){
	var oReg=new RegExp(/^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[\w-\.]*[a-zA-Z0-9]\.[a-zA-Z]{2,7}$/);
	if(oReg.test(val)||val==""){
		return true;
		}
	else{
		return false;
	}
}

function valDate(dateStr){
	if(dateStr==""){return true;}
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is format OK?
	if (matchArray == null) {
	  //alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
	  return false;
	}

	month = matchArray[1];
	day = matchArray[3];
	year = matchArray[5];

	if (month < 1 || month > 12) {
	  //alert("Month must be between 1 and 12; please enter a valid date.");
	  return false;
	}
	if (day < 1 || day > 31) {
	  //alert("Day must be between 1 and 31; please enter a valid date.");
	  return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
	  alert("Month " + month + " doesn't have 31 days; please enter a valid date.")
	  return false;
	}
	if (month == 2) {
	  var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
	  if (day > 29 || (day==29 && !isleap)) {
	    alert("February " + year + " doesn't have " + day + " days; please enter a valid date.");
	    return false;
	  }
	}
	return true;
}
