/*
 * @ctype file
 * @id /locator.js
 * @name locator.js
 * @author Frances Puckett
 * @version $Revision:   4.8  $
 * @file $RCSfile$
 * @revdate $Date:   Feb 03 2004 10:29:30  $
 */

function validateInfo(){
	var city = document.forms["initial"].CITY.value;
	var state = document.forms["initial"].STATE.options[document.forms["initial"].STATE.selectedIndex].value;
	var zip = document.forms["initial"].ZIP.value;
	var country;
	if (document.forms["initial"].AD4.value) {
			country = document.forms["initial"].AD4.value;
	}
	else{
			country = document.forms["initial"].AD4.options[document.forms["initial"].AD4.selectedIndex].value; 
	}
	if ((isEmpty(city) && state == "") && isEmpty(zip)){
		if (country == "CAN"){
			alert("Please choose at least a province or a postal code.");
			return false;
		}
		else if (country == "USA"){
			alert("Please choose at least a state or a ZIP code.");
			return false;
		}
	}
	if (!isEmpty(city)){
		if (state == ""){
			if (isEmpty(zip)){
				if (country == "USA"){
					alert("Please choose either a state or a ZIP code.");
					return false;
				}
				else if (country == "CAN"){
					alert("Please choose either a province or a postal code.");
					return false;
				}
			}
		}
	}
	if (!isEmpty(zip)){
		if (country == "USA"){
			if (!isZIPCode(stripCharsInBag(zip, " -"))){
				if (isCanadianPostalCode(stripCharsInBag(zip, "- "))){
					alert("To find information for a Canadian store, please visit www.homedepot.ca");
				}
				else{
					alert("The ZIP Code is not a valid US ZIP Code.");
				}
				document.forms["initial"].ZIP.focus();
				return false;
			}
		}
		else if (country == "CAN"){
			if (!isCanadianPostalCode(stripCharsInBag(zip, "- "))){
				alert("The Postal Code is not a valid Canadian Postal Code.");
				document.forms["initial"].ZIP.focus();
				return false;
			}
		}
	}
	document.forms["initial"].frmAction.value = "getStores";
	return true;
}
function storeNumberValidateInfo(thisForm){
	var myForm;
	if (thisForm){
		myForm = thisForm;
	}
	else {
		myForm = document.forms["storeNumberForm"];
	}
	var storeNumber = myForm.targetedBizID.value;
	
	if (!isEmpty(storeNumber)){
        if(isInteger(storeNumber)){
					myForm.frmAction.value = "getTargetedBizID";
					return true;
				}
				else {
					alert("The store number can only contain numbers.");
					myForm.targetedBizID.focus();
					return false;
				}	
	
	}
	else if(isEmpty(storeNumber)){
		alert("This field can not be empty");
	  myForm.targetedBizID.focus();
		return false;
	}
}
function validateZip(thisForm){
	var myForm;
	if (thisForm){
		myForm = thisForm;
	}
	else {
		myForm = document.forms["storeFinderForm"];
	}
	
	var zip = myForm.ZIP.value;
	var country = myForm.AD4.value;
 	if (!isEmpty(zip)){
			if (isZIPCode(stripCharsInBag(zip, " -"))){
				myForm.AD4.value = "USA";
				myForm.frmAction.value = "getStores";
				return true;
			}
		 if (isCanadianPostalCode(stripCharsInBag(zip, "- "))){
				alert("To find information for a Canadian store, please visit www.homedepot.ca");
				return false;
		}		
		 else {
				alert("The ZIP Code is not a valid US ZIP Code.");
				myForm.ZIP.focus();
				return false;
			}
			
	}
	else if(isEmpty(zip)){
		alert("This field can not be empty");
	  myForm.ZIP.focus();
		return false;
	}
	 
}
function chooseCountryFromState(field){
		if (eval(document.forms["initial"].STATE.selectedIndex) < 53){
			document.forms["initial"].AD4.options[0].selected = true ;
		}
		else {
			document.forms["initial"].AD4.options[1].selected = true ;
		}
	}
			
function chooseCountryFromZIP(field){
		var zip = field.value;
		if (isZIPCode(stripCharsInBag(zip, " -"))){
			document.forms["initial"].AD4.options[0].selected = true ;
		}
		if (isCanadianPostalCode(stripCharsInBag(zip, "- "))){
			document.forms["initial"].AD4.options[1].selected = true ;
		}
	}
function chooseState(state){
	if (state){
		document.forms["initial"].STATE.value = state;
		document.forms["initial"].MULTI.value = "yes";
	}
	document.forms["initial"].submit();
}
function goPan(formName, dir){
	var thisForm = eval('document.forms["' + formName + '"]');
	thisForm.frmAction.value = "Pan";
	thisForm.PN.value = dir;
	thisForm.submit();
}
function goZoom(formName, dir){
	var thisForm = eval('document.forms["' + formName + '"]');
	thisForm.frmAction.value = "Zoom";
	var zoomLevel = parseInt(thisForm.zoomLevel.value);
	var zoomIn = 1;
	var zoomOut = 11;
	if (zoomLevel != 1){ 
		zoomIn = zoomLevel - 1;
	}
	if (zoomLevel != 11){
		zoomOut = zoomLevel + 1;
	}
		
	if (dir == "in"){
		thisForm.LV.value = zoomIn;
	}
	else {
		thisForm.LV.value = zoomOut;
	}
	thisForm.submit();
}
function getDriveDirections(formName, storeID){	//storeID currently only used so that the link does not display "visited" unless it really is.
	var thisForm = eval('document.forms["' + formName + '"]');
	thisForm.submit();
}
function validateDrive(){	
	var address = document.driveDir.OAD2.value;
	var OCITY = document.driveDir.OCITY.value;
	var OSTATE = document.driveDir.OSTATE.options[document.driveDir.OSTATE.selectedIndex].value;
	var OZIP = document.driveDir.OZIP.value;
	if ((OCITY == null || OCITY == "") && (OSTATE == null || OSTATE == "") && (OZIP == null || OZIP == "")){
		alert("You must choose at least a city and state or a ZIP code");
		return false;
	}
	if (address != null && address != ""){
		if ((OCITY == null || OCITY == "") && (OSTATE == null || OSTATE == "")){
			if (OZIP == null || OZIP == ""){
				alert("You must select either a city and state or a zip.");
				return false;
			}
		}
	}
	if (OCITY != null && OCITY != ""){
		if (OSTATE == null || OSTATE == ""){
			if (OZIP == null || OZIP == ""){
				alert("You must select either a state or a ZIP code.");
				return false;
			}
		}
	}
	if (OSTATE != null){
		if (OCITY == null || OCITY == ""){
			if (OZIP == null || OZIP == ""){
				alert("You must select either a city or a ZIP code.");
				return false;
			}
		}
	}
	if (OZIP != null && OZIP != ""){
		if (!isZIPCode(stripCharsInBag(OZIP, " -"))){
			alert("The ZIP Code is not a valid US ZIP Code.");
			return false;
		}
	}
	if (OCITY == null){
		OCITY = "";
	}
	if (OSTATE == null){
		OSTATE = "";
	}
	if (OZIP == null){
		OZIP = "";
	}
	
	document.driveDir.OAD3.value = OCITY + " " + OSTATE + " " + OZIP;
	return true;
}
function chooseAddressFromMulti(oad2, oad3, driveQuery, approx){	
	if (oad2 != null && oad2 != "null" && oad2 != ""){
		document.forms["driveDir"].OAD2.value = oad2;
	}
	if (oad3){
		document.forms["driveDir"].OAD3.value = oad3;
	}
	if (driveQuery){
		document.forms["driveDir"].driveQuery.value = driveQuery;
	}
	if (approx){
		document.forms["driveDir"].approx.value = approx;
	}
	document.forms["driveDir"].getApproxDirections.value = "ok";
	document.forms["driveDir"].submit();
}
function setManeuversValue(maneuverFlag){
	document.maneuversForm.maneuvers.value = maneuverFlag;
	document.maneuversForm.submit();
}
function showEnlargedManeuver(manTxt,mapImage,copyright){
	document.maneuvers2Form.manTxt.value = manTxt;
	document.maneuvers2Form.mapImage.value = mapImage;
	document.maneuvers2Form.submit();
}
function GoToVicinity(URL) {
  popupWin = window.open(URL, 'Vicinity', 'resizable=yes,scrollbars,width=675,height=675,screenX=10,screenY=10')
	  window.top.name='opener';
	popupWin.focus();
}
function isEmpty(s){   
	return ((s == null) || (s.length == 0))
}
function isZIPCode (s){  
	return (isInteger(s) && 
		((s.length == 5) ||
		(s.length == 9)));
}
function isCanadianPostalCode(pc){
	var canadian = /[A-Z]\d[A-Z]\s*\d[A-Z]\d/i;
	if (!pc) return false ;
	if (pc.match(canadian)){
		return true;
	}
	else{
		return false;
	}
}
function stripCharsInBag (s, bag){ 
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}
function isInteger (s){   
	for (var i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if (!isDigit(c)) return false;
	}
	return true;
}
function isDigit (c){   
	return ((c >= "0") && (c <= "9"))
}
function mkt_isLetter (c){   
    return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}
function isCanadaPostalCode(zipCode){   
   if ( !mkt_isLetter( zipCode.charAt(0) ) ){
        return false;
    }
   if ( zipCode.length < 6 ){
        return false;
    }
   return true;
}
function validate_getZip(){   
    var thisForm = document.frm_getZip;
    var zipCode  = thisForm.ZIP_POSTAL_CODE.value;
    if (zipCode == null || zipCode == "") {
        alert("Please enter a valid Zip code.");
        return false;
    }
    if ( !isCanadaPostalCode(zipCode) && ( zipCode.length > 5 || isNaN(parseInt(zipCode)) ) ) {
        alert("Please enter a valid ZIP code.");
        return false;
    }
    if ( zipCode.length < 5 ){
        alert("Please enter a valid Zip code.");
        return false;
    }
    if ( isCanadaPostalCode(zipCode) ){
        thisForm.CANADA_POSTAL_CODE.value = "true"; // Indicate that this is a Canadian Postal Code.
        return true;
    }
    else
        thisForm.CANADA_POSTAL_CODE.value = "false";
    var zip = parseInt(zipCode);    
    if (isNaN(zip)){
        alert("Please enter a valid Zip code...");
        return false;
    }
    var expdate = new Date ();
    expdate.setTime (expdate.getTime() + (1000 * 60 * 60 * 24 * 365));
    setCookie ("HDZipcode", zipCode, expdate);
    return true;
}
function submit_getZip(showZipFinder){
    var whichForm = document.frm_getZip ;
    if (showZipFinder) {   
        whichForm.ZIP_FINDER.value = "1" ;
        whichForm.submit();
    }   
    else
    if ( !validate_getZip() ){
        whichForm.ZIP_POSTAL_CODE.select();
        whichForm.ZIP_POSTAL_CODE.focus();
    }
    else {
        whichForm.submit();
    }   
}
function gotoHomePage(){
    document.frm_getEMail.NEXT_ACTION.value='HOMEPAGE'; 
    document.frm_getEMail.submit();
    deleteCookie("HDZipcode");
}
function submit_getEMail(){   
    var whichForm = document.frm_getEMail ;
    if ( !validate_getEMail() ) {
        whichForm.NEW_EMAIL_ADDRESS.select();
        whichForm.NEW_EMAIL_ADDRESS.focus();
    }
    else {
        showPopup_getEMail();
        whichForm.submit();
    }   
}
function validate_getEMail(){   
    var thisForm = document.frm_getEMail;
    var mailId = thisForm.NEW_EMAIL_ADDRESS.value;  
    if (mailId == null || mailId == "") {
        alert("An entry is required in the email field.");
        return false;
    }
    var index1 = mailId.indexOf("@");   
    var index2 = mailId.lastIndexOf(".");
    if ( ( index1 == -1 ) || ( index2 == -1 ) ) {
        alert("Invalid e-mail address.");
        return false;
    }
    if ( index1+1 >= index2 ){
        alert("Invalid e-mail address.");
        return false;
    }   
    if ( index2 >= mailId.length-1 ){
        alert("Invalid e-mail address.");
        return false;
    }
    if ( index1 == 0 ) {
        alert("Invalid e-mail address.");
        return false;
    }
    var indx = mailId.indexOf(" ");
    if ( indx != -1 ) {
        alert("Invalid e-mail address.");
        return false;
    }
    return true;
}
function openZip(URL,title,w,h){
        var popupWin = window.open(URL, title, 'resizable=yes,scrollbars,screenX=10,screenY=10,width='+w+',height='+h);
        window.top.name='opener';
        popupWin.focus();
}
function setCookie (name, value, expires) {
    if (expires) {
        document.cookie = name + "=" + escape (value) +"; expires=" + expires.toGMTString() +  "; path=/";
    } else {
        document.cookie = name + "=" + escape (value) +"; path=/";
    }
}
function getCookie (name) {
    var dcookie = document.cookie; 
    var cname = name + "=";
    var clen = dcookie.length;
    var cbegin = 0;
    while (cbegin < clen) {
        var vbegin = cbegin + cname.length;
        if (dcookie.substring(cbegin, vbegin) == cname) { 
            var vend = dcookie.indexOf (";", vbegin);
            if (vend == -1) vend = clen;
            return unescape(dcookie.substring(vbegin, vend));
        }
        cbegin = dcookie.indexOf(" ", cbegin) + 1;
        if (cbegin == 0) break;
    }
    return null;
}
function deleteCookie(name) {
    
    document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/";
}



