//This javascript file contains functions commonly used throughout the site
//including preloadImage() for nav rollovers
//several functions to handle highliting of present nav section
//a generic popUpWin() to launch page in new window

var historyclickdelay=1000;


//determine which section this file is in
var tarray,docnav,subnav,tertiarynav,quaternarynav;
docnav=""; subnav=""; tertiarynav=""; quaternarynav="";

/*if (pageNum) {
	tarray = pageNum.split("."); 
	if (tarray.length>0) {
		docnav = tarray[0];
		if (tarray.length>1) {
			subnav = tarray[1];
			if (tarray.length>2) {
				tertiarynav = tarray[2];
			    if (tarray.length>3) {
				    quaternarynav = tarray[3];
			    }
            }
		}
	}
}*/

var qstring=location.search;


// Preload Nav Images
var images_array = new Array("/images/mainnav/bridal_gowns_on.gif","/images/mainnav/bridal_party_on.gif","/images/mainnav/accessories_on.gif","/images/mainnav/locations_on.gif","/images/mainnav/special_events_on.gif","/images/mainnav/priscilla_experience_on.gif","/images/mainnav/home_footer_on.gif","/images/mainnav/styleguide_footer_on.gif","/images/mainnav/newspress_footer_on.gif","/images/mainnav/about_footer_on.gif","/images/mainnav/employment_footer_on.gif","/images/mainnav/privacypolicy_footer_on.gif");

function preloadNavImages() {
    for(loop = 0; loop < images_array.length; loop++) {
        var an_image = new Image();
        an_image.src = images_array[loop];
    }
}

//preload other images on page
function preloadImages2(tArray) {
    for(loop = 0; loop < tArray.length; loop++) {
        var an_image = new Image();
        an_image.src = tArray[loop];
    }
}

// Generic popUpWin() function, takes 3 params: url,name,windowprops
function popUpWin (myUrl,myWinName,myWinProps) {
	//alert(myUrl+","+myWinName+","+myWinProps);
	myNewWin = window.open(myUrl,myWinName,myWinProps);
	myNewWin.focus();
}


// Match drop down box to selected value
// setSelect(this,value)
function setSelect(theObject,a) {
	for (i=0;i<theObject.options.length;i++) {
		if (theObject.options[i].value==a) {
			theObject.selectedIndex=i;
			break;
		}
	}
}


// toggle Checkbox on/off by clicking adjacent text
// useage toggleCheckbox( formname a , elementname b )
function toggleCheckbox(a,b) {
    if (document.forms[a].elements[b].checked==false) {
        document.forms[a].elements[b].checked = true;
    } else {
        document.forms[a].elements[b].checked = false;
    }
}

// toggle to radio button by clicking adjacent text
// usage toggleRadio( formname a, elementname b, button_order c)
function toggleRadio(a,b,c) {
    //alert(document.forms[a].elements[b][c].checked);
    if (document.forms[a].elements[b][c].checked==false) {
        document.forms[a].elements[b][c].checked=true;
    }
}


function checkforRadio(theForm,q) {
    for (i=0;i<theForm.elements[q].length;i++) {
        if (theForm.elements[q][i].checked==true) {
            //alert(theForm.elements[q][i].value);
            return true;
            break;
        }
    }
    return false;
}


function checkMaxSelect (theForm,theBox,boxOrder,maxCks) {
    //alert(theForm.elements[theBox].length);
    tcnt=0;
    
    for (i=0; i<theForm.elements[theBox.name].length; i++) { 
        if (theForm.elements[theBox.name][i].checked==true) {
            tcnt++;
        }
        if (tcnt > maxCks) {
            theForm.elements[theBox.name][boxOrder].checked=false;
            alert("Please select only " + maxCks + " items.");
            break;
        }
    }
}

//clear text box on click
function clearField2(a,b) {
	if (a.value==b) {
		a.value="";
	}
    //a.select();
}


function RTrim(strMyString) {
    return(strMyString.replace(/^\s*/,""));
}
 
function LTrim(strMyString) {
    return(strMyString.replace(/\s*$/, ""));
}
 
function Trim(strMyString) {
    return(RTrim(LTrim(strMyString)));
}

// check for valid email
function isEmail(email) {
    invalidChars = " ~\'^\`\"*+=\\|][(){}$&!#%/:,;";

    // Check for null
    if (email == "") {
        return true;
    }

    // Check for invalid characters as defined above
    for (i=0; i<invalidChars.length; i++) {
        badChar = invalidChars.charAt(i);
        if (email.indexOf(badChar,0) > -1) {
            return false;
        }
    }
    lengthOfEmail = email.length;
    if ((email.charAt(lengthOfEmail - 1) == ".") || (email.charAt(lengthOfEmail - 2) == ".")) {
        return false;
    }
    Pos = email.indexOf("@",1);
    if (email.charAt(Pos + 1) == ".") {
        return false;
    }
    while ((Pos < lengthOfEmail) && ( Pos != -1)) {
        Pos = email.indexOf(".",Pos);
        if (email.charAt(Pos + 1) == ".") {
            return false;
        }
        if (Pos != -1) {
            Pos++;
        }
    }

    // There must be at least one @ symbol
    atPos = email.indexOf("@",1);
    if (atPos == -1) {
        return false;
    }

    // But only ONE @ symbol
    if (email.indexOf("@",atPos+1) != -1) {
        return false;
    }

    // Also check for at least one period after the @ symbol
    periodPos = email.indexOf(".",atPos);
    if (periodPos == -1) {
        return false;
    }
    if (periodPos+3 > email.length) {
        return false;
    }
    return true;
}


function MM_openBrWindow(theURL,winName,features) { //v2.0
  var mywin=window.open(theURL,winName,features);
  mywin.focus();
}


function getAnchorIdNum(myanchor) {
    for (i=0 ; i < document.anchors.length ; i++) {
        if (document.anchors[i].name == myanchor) {
            return(i);
            break;
        }
    }
    return(-1);
}


var theForm;
var requestSubmitted = false;  

function disableButton(btn,form,buttonType) {
   if (!requestSubmitted){
        if (buttonType != null) {
           var buttonName = buttonType;
           btn.src = buttonName.src; // image swap happens here
        } else {
           var submitMessage = "  Please Wait...  ";
           btn.value = submitMessage;
        }
        theForm = form;
        btn.disabled = true;
        requestSubmitted = true;
        setTimeout("submitIt()", 1);
   } else {
        return false;
   }
}

function submitIt() {
    theForm.submit();
    return false;
}

function checkandsubmit(submitForm) {
    if(requestSubmitted == true) {
         return false;
    } else {
        requestSubmitted = true;
        submitForm.submit();
        return false;
    }
}


function strrev(str) {
   if (!str) return '';
   var revstr='';
   for (i = str.length-1; i>=0; i--) {
       revstr+=str.charAt(i)
   }
   return revstr;
}


function textLimit(field, maxlen) {
    if (field.value.length > maxlen) {
        field.value = field.value.substring(0, maxlen);
        //alert("your input has been truncated!");
    }
}


// doNothing function to replace <a href="#">
// use <a href="javascript:doNothing();"> instead
function doNothing() {
    void(0);
}

// Popup function for Editors' Picks video (launched by Flash only as of 12/16/2004)
function popvideo(win, name) {
	window.open(win, name, 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=500,height=420,left = 60,top =10');
}


function sendOpenerTo(mypage) {
    if (typeof(opener)=="object") {
        if (opener.closed) {
            var winparent = window.open(mypage,'parentwindow','');
   	    } else {
            opener.location=mypage;
        }
    } else {
        var winparent = window.open(mypage,'parentwindow','');
    }
    window.close();
}


function sendTopTo(myURL) {
    top.location=myURL;
}