//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

//determine which section this file is in
var tarray,docnav,subnav,subnav2;
docnav=""; subnav=""; subnav2="";

if (typeof(pageNum)=="string") {
	tarray = pageNum.split("."); 
	if (tarray.length>0) {
		docnav = tarray[0];
		if (tarray.length>1) {
			subnav = tarray[1];
			if (tarray.length>2) {
				subnav2 = tarray[2];
			}
		}
	}
}

var qstring=location.search;

var navArray = new Array;
// navbar (topnav) images
navArray[0] = 'nav_home';
navArray[1] = 'nav_experiencedavids';
navArray[2] = 'nav_mydressingroom';
navArray[3] = 'nav_winagown';
navArray[4] = 'nav_davidsbridalrewards';
navArray[5] = 'nav_specialoffers';
navArray[6] = 'nav_events';
navArray[7] = 'nav_showcaseofbrides';
navArray[8] = 'nav_giftregistry';
navArray[9] = 'nav_bridalgowns';
navArray[10] = 'nav_bridesmaids';
navArray[11] = 'nav_tuxedos';
navArray[12] = 'nav_flowergirl';
navArray[13] = 'nav_accessories';
navArray[14] = 'nav_specialoccasions';
navArray[15] = 'nav_weddingdayessentials';
// footer text links only
navArray[16] = 'services';
navArray[17] = 'findastore';
navArray[18] = 'FAQ';
navArray[19] = 'aboutus';
navArray[20] = 'privacy';
navArray[21] = 'sitemap';
navArray[22] = 'styleguide';
navArray[23] = 'nav_prom';
navArray[24] = 'catalog';



// makes topnav images "highlite" for currect section
function setNav() {
    //alert(docnav);
	if ((docnav > 0 && docnav < 16) || docnav==23) {
		document.images[navArray[docnav]].src='/images/topnav/'+navArray[docnav]+'_lit.gif';
	}
}

// makes footer text links "active" for current section
function setFooterNav(x) {
	//alert(docnav + " " + x);
	//alert(x);
	if (docnav == x && docnav + "." + subnav != '19.2') {		
		document.write("<span class=\"navtextActive\">");
	} else if (pageNum == x) {
		//special case for order catalog, a subnav of promotions, but in the footer
		document.write("<span class=\"navtextActive\">");
	} else if (docnav + "." + subnav == x) {
		//special case for join us, a subnav of about us, but in the footer
		document.write("<span class=\"navtextActive\">");
	} else {
		document.write("<span>");
	}
}


// makes subnav text links "active" for current section
function setSubNav(x) {
	//alert(subnav + " " + x);
	if (subnav == x) {
		document.write("<span class=\"subnavtextActive\">");
	} else {
		document.write("<span>");
	}
}


// makes subnav text links "active" for bridal and accessories sections
function setSubNav2(x) {
	//alert(qstring + " " + x);
	if (qstring.indexOf(x) > -1) {
		document.write("<span class=\"subnavtextActive\">");
	} else {
		document.write("<span>");
	}
}


// makes subnav text links "active" for current section
function setSubNav3(x) {
	//alert(subnav + " " + x);
	if (subnav2 == x) {
		document.write("<span class=\"subnavtextActive\">");
	} else {
		document.write("<span>");
	}
}

// unhlite function leaves the appropriate nav section lit on mouseout
function navUnHlite(x) {
	if (((x >= 0 && x < 16) || x==23) && docnav != x) {
		document.images[navArray[x]].src='/images/topnav/'+navArray[x]+'.gif';
	}
}



/// Preload Nav Images
//Preload Images
/*var images_array = new Array('../images/topnav/nav_experiencedavids_lit.gif','../images/topnav/nav_mydressingroom_lit.gif','../images/topnav/nav_winagown_lit.gif','../images/topnav/nav_davidsbridalrewards_lit.gif','../images/topnav/nav_specialoffers_lit.gif','../images/topnav/nav_events_lit.gif','../images/topnav/nav_showcaseofbrides_lit.gif','../images/topnav/nav_giftregistry_lit.gif','../images/topnav/nav_bridalgowns_lit.gif','../images/topnav/nav_bridesmaids_lit.gif','../images/topnav/nav_flowergirl_lit.gif','../images/topnav/nav_tuxedos_lit.gif','../images/topnav/nav_specialoccasions_lit.gif','../images/topnav/nav_prom_lit.gif','../images/topnav/nav_accessories_lit.gif','../images/topnav/nav_weddingdayessentials_lit.gif');

function preloadImages() {
    for(loop = 0; loop < images_array.length; loop++) {
        var an_image = new Image();
        an_image.src = images_array[loop];
    }
    //Call to highlite current nav section (topnav only)
    //setNav();
    if (initMenus()) {
	;
	}
}
*/


// 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();
}


// sendOpenerTo()
// safe open page in parent, checks if opener exits
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;
}


// 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;
        }
    }
}


    //Define global variables
        var timerID = null;
		var timerOn = false;
		var timecount = 1000;
		var what = null;
		var newbrowser = true;
		var check = false;
        var xd = 0;
        

function initMenus(){
    	// alert ("Running Init");
          if (document.layers) {
                      //  alert ("Running Netscape 4");
                        layerRef="document.layers";
                        styleSwitch="";
                        visibleVar="show";
			screenSize = window.innerWidth;
			what ="ns4";


          } else if(document.all){
                      //  alert ("Running IE");
                        layerRef="document.all";
                        styleSwitch=".style";
                        visibleVar="visible";
			screenSize = document.body.clientWidth + 18;
			what ="ie";

		  } else if(document.getElementById){
                      //  alert ("Running Netscape 6");
                        layerRef="document.getElementByID";
                        styleSwitch=".style";
                        visibleVar="visible";
			what="moz";
		  
		  } else {
		  	//alert("Older than 4.0 browser.");
			what="none";
			newbrowser = false;
		  }
          
        //check = true;
        centerAllMenus();
}


var subNavOffsetLeft = new Array();

subNavOffsetLeft[0] = 0;




function centerAllMenus() {
    //alert("centering menus");
	if (document.layers) {
		 if (window.innerWidth > 620) {
			xd = (window.innerWidth - 620) / 2;
		 }
		 /*if (window.innerHeight > 365) {
			yd = (window.innerHeight - 365) / 2;
		 }*/
		 for (i=1; i<7; i++) {
			document.layers["subnav"+i].left += xd;
			//document.layers[i].top += yd;
		    //document.layers[i].left += 2;
		 }
	} else if (document.all && !document.getElementsByTagName) {
		if (document.body.offsetWidth > 620) {
		    xd = (document.body.offsetWidth - 620) / 2;
		}
		/*if (document.body.offsetHeight > 365) {
		   yd = (document.body.offsetHeight - 365) / 2;
		}*/
		var theLeft;
		var theTop;
		var infolayers = ['subnav1','subnav2','subnav3','subnav4','subnav5','subnav6'];
		for (i=0;i<infolayers.length;i++) {
			eval('theLeft = parseInt(document.all.'+ infolayers[i] + '.style.left)');
			theLeft += xd;
			theLeft += 'px';
			eval('document.all.' + infolayers[i] + '.style.left = theLeft');
			/*eval('theTop = parseInt(document.all.' + infolayers[i] + '.style.top)');
			theTop += yd;
			theTop += 'px';
			eval('document.all.' + infolayers[i] + '.style.top = theTop');
		    */
        }
		//document.all.imcmain.style.visibility = visible;
	} else if (document.getElementsByTagName) {
		var aDivs = document.getElementsByTagName("div");
		if (document.all) {
            //alert(document.body.offsetWidth);
			if (document.body.offsetWidth > 760) {
				//xd = (document.body.offsetWidth - 760) / 2;
                //xd -= 10;
                xd = -9;
			}
			/*if (document.body.offsetHeight > 365) {
				yd = (document.body.offsetHeight - 365) / 2;
			}*/
		} else {
			if (window.innerWidth > 760) {
				xd = (window.innerWidth - 760) / 2
			}
			/*if (window.innerHeight > 365) {
				yd = (window.innerHeight - 365) / 2
			}*/
		}
        
		for (i=1; i < 7; i++) {
			//aDivID=aDivs[i].id;
			//document.getElementById("subnav"+i).style.left = parseInt(document.getElementById("subnav"+i).style.left) + xd + "px";
			document.getElementById("subnav"+i).style.left = (getRealPos(document.images["dbmainlogo"],"Left") + xd) + "px";
            //alert(document.getElementById("subnav"+i).style.left)
            //document.getElementById(aDivID).style.top = parseInt(document.getElementById(aDivID).style.top) + yd + "px";
			//if (aDivs[i].id == "imcmain" || aDivs[i].id == "info0") {
			//	document.getElementById(aDivID).style.visibility = "visible";
			//}
            //alert(i+":"+document.getElementById("subnav"+i).style.left);
		}
	}
    
    check = true;
    
    if (docnav>0 && docnav<7) {
        showLayer("subnav"+docnav);
    }

    //alert("menus centered");
}


// 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;
}


// doNothing function to replace <a href="#">
// use <a href="javascript:doNothing();"> instead
function doNothing() {
    void(0);
}

function winResize() {
    //alert("onresize fired");
    centerAllMenus();
}