// core.js - basic, global javascript functions.  JB
//------------------------------------------------\\

var loaded = false;				  // set to true when init is called and any body onload logic is done
var imageSources = new Array();	  // add in all the image references that should be preloaded (i.e. for rollovers)
var category = "";
var familyName = "";			  
var currValue,currField,nextField;
var grKeep = false;               //set to true for GR DHTML
var familyID = 0;


//Stub for ShowCart in Checkout Situations
function ShowCart() {}

//init - initialize any javascript objects or functions.  
//
//DHTML should loop through here if necessary.  
//bBlockFocus and bBlockPreload are optional.
//Should be called by the body OnLoad event.
function init(bBlockFocus,bBlockPreload,bBlockImageUpdate) {
  if(!bBlockPreload) {
     preloadImages();
  }

  if(!bBlockFocus) {
     focusNextField();
  }
  
  if(!bBlockImageUpdate) {
  //  updateImages();
  }
  
  loaded = true; 
}

function hideBlocks(){}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function focusNextField() {}

//this function is set by phone fields for auto-tabbing
function setWait(c,n) {
	 c.focus();			//insures focus of the current field
	 c.select();		//selects what was previously there for editing
	 
	 currValue = c.value;  //previous value, so we can check for it being changed
	 currField = c;		   //current field	
	 nextField = n;		   //field to jump to
	 
	 setTimeout("checkForNum()",100);
	 }

//auto tab when we have a value of the correct length and different from before
function checkForNum() {  
   if(currField.value.length == 3 && currField.value != currValue) {nextField.focus();}
   else{setTimeout("checkForNum()",1);}
   }

String.prototype.toInt = function() {
    var a = new Array();
    for (var i = 0; i < this.length; i++) {
        a[i] = this.charCodeAt(i);
    }
    return a;
}

function updateImages() { /*
  if(updateSources && updateSources.length > 0) {
  x = 0;
  for (var i = 0; i < document.images.length; i++) {
       if( x < updateSources.length && document.images[i].src.indexOf(updateSources[x]) > 0 ) {
            document.images[i].src = document.images[i].src + "?" + updateTag;
            window.status =  document.images[i].src;
            x++;
           }
    }
  }
*/
}

//this function is set by the gift message for auto-tabbing
function setWordWrap(c) {
//	 c.focus();			//insures focus of the current field
//	 c.select();		//selects what was previously there for editing
	 r = parseInt(c.name.charAt(17));
	 m = parseInt(c.name.charAt(30)) + 1;
//	 alert(c.name.charAt(17));
	 n = eval("document.giftoptions.rptRecipient__ctl"+r+"_textMsgLine"+m);
//	 alert(r);
	 if ( n.value != "" ) {return;}
	 
	 currValue = c.value;  //previous value, so we can check for it being changed
	 currField = c;		   //current field	
	 nextField = n;		   //field to jump to

	 setTimeout("checkForWords()",100);
	 }

//auto tab when we have a value of the correct length and different from before
function checkForWords() {  
//   window.status = currField.value.length;
   if(currField.value.length == 25 && currField.value != currValue && nextField.value == "" && currField.value.indexOf(" ") > 0) {
      myWords = currField.value.split(" ");
      oldField = currField;
      newField = nextField; // odd, but Moz (correctly) will shift this value after the next line.
      nextField.select();
      newField.value = myWords[myWords.length - 1] + newField.value;
      myWords.length = myWords.length-1;
      oldField.value = myWords.join(" ");
     // newValue = nextField.value;
      
    //  currField.blur();
   //   nextField.select();
      
    
      }
   else if (nextField.value == ""){setTimeout("checkForWords()",1);}
   }

//cycle through imageSources[] and preload the images into the browser cache
function preloadImages() {
	//alert("preloading images");
	imageHolders = new Array();
	
	if(!document.getElementById&&document.all){return true;}
	
	//alert("document.images: " + document.images + "; imageSources.length: " + imageSources.length);
	if (document.images) {
		for(x=0;x<imageSources.length;x++) {
		    imageHolders[x] = newImage(imageSources[x]);	
	    }
    }
}

function getNextURL(anchor) {
   for(x=0;x<document.links.length;x++) {
       if(document.links[x] == anchor) {
          return document.links[x+1].href;
          }
   }
}

//swap the src of document.imageName with imageSrc.  For rollovers.  Optionally, send the image reference itself.
function changeImages(imageName,imageSrc,imageRef) {
	//alert("Changing images: " + imageName + "; " + imageSrc + "; " + imageRef);
	if(!document.getElementById&&document.all){return true;}
	
	if (document.images && (loaded == true)) {
		if(imageRef) {
		   if(imageRef.src.indexOf("spacer.gif") > 0) { return true; }  //ignore invisible images
		   imageRef.src = imageSrc;
		   } else {
		   if(document[imageName].src.indexOf("spacer.gif") > 0) { return true; } //ignore invisible images
		   document[imageName].src = imageSrc;
		   }
	}
}

// function used to open links in search results (keyword mappings)
function openExternalLink(p_sExternalLink)
{
	var url = p_sExternalLink;
	var hasCompleteURL = url.indexOf('http://')
	if (hasCompleteURL==-1)
	{
		url = "http://" + url;
	}
	var opts = "scrollbars=yes,toolbar=yes,location=yes,resizable=yes,status=yes,width=800,height=600,";
	var winOpen = window.open(url,"win",opts);
}

// Generic PopUp Opener, use type to switch options.  newURL and newVars are optional.
function openPopUp(newURL,type,newQVars,useGlobals,skipLoader,realHeight) {  
var url;  //location of popup content
var h;   //top
var w;   //left
var h;   //height
var w;   //width
var opts; //options


	url = "underconstruction.htm";   // default settings here
	t = 50;
	l = 50;
	h = 560;
	w = 525;
	opts = "resizable=yes, toolbar=no, location=no, status=no, scrollbars=yes, menubar=no, titlebar=no"

	switch (type)
    {
    case "sample": { url = "list.htm"; h=400;  break }
    case "larger": { w=560; h=620;  break }
	  case "xl": { w=750; h=550; l=10; opts="resizable=yes, toolbar=yes, location=no, status=no, scrollbars=yes, menubar=yes, titlebar=no"; break  }
	  case "short": { h=250; break }
	  case "gift": { h=350; break }
	  case "wide": { w=620; break }
    case "narrow": { w=490; break }
	  case "hidef": { h=520; w=760; t=25; l=0; }
    default: { break }  // probably won't be needed with the above, but just in case
    }
	if(realHeight) { h = realHeight; }
    if (newURL && !skipLoader) {
        url = newURL + "?";
    //    alert(url);
        if(!useGlobals) {
			url = url + "&c=" + category + "&f=" + familyID;
		}
        if(newQVars){url += "&" + newQVars;}
	} else { url = newURL; }
        
    opts = "top="+t+",left="+l+",width="+w+",height="+h+","+opts
	
	// append family name (if needed)
	if(familyName != "") {url += "&n=" + escape(familyName);}
	
//	winpops=window.open("/popup/loading.htm","popup",opts)
//	winpops.close();
//    alert(url);

	winpops=window.open(url,"popup",opts);
	resizeWindowTo(h,w,winpops);
	winpops.focus();
	
	return;
}

function resizeWindowTo(h,w,win) {
// alert(win);
 win.resizeTo(w,h);
 
}

function changeParentURL(url) {
 window.location.href = url;
}


function setAddresses(address,newCity,newState,newZip,newKeepOriginal) {
	if(!newCity) { cityElement = document.frmAddress.txtCity;} else {cityElement = newCity; }
	if(!newState) { stateElement = document.frmAddress.ddlState;} else {stateElement = newState;}
	if(!newZip) { zipElement = document.frmAddress.txtZip;} else {zipElement = newZip;}
	if(!newKeepOriginal) { hdnElement = document.frmAddress.hdnKeepOriginalAddress;} else {hdnElement = newKeepOriginal;}

	
    
	addressArray = address.split("|");
	if (addressArray.length < 2) { 
		return;
	}
	if (document.frmAddress['messagingArea:msgDropDown'].selectedIndex == 1) {
		hdnElement.value = "true";
	} else {
		hdnElement.value = "false";
	}

	//this is for street level validation
	//document.frmAddress.txtAddress1.value = addressArray[0];
	//document.frmAddress.txtAddress2.value = addressArray[1];
	//document.frmAddress.txtCity.value = addressArray[2];
	//document.frmAddress.txtZip.value = addressArray[4];
	//document.frmAddress.txtZipExt.value = addressArray[5];

	//this is for city/state/zip validation
	cityElement.value = addressArray[0];
	zipElement.value = addressArray[2];
	
	for(i=0; i<stateElement.options.length; i++) {
		//this is for street level validation
		//if (document.frmAddress.ddlState.options[i].value == addressArray[3]) {
		//this is for city/state/zip validation
		if (stateElement.options[i].value == addressArray[1]) {
			stateElement.options[i].selected = true;
		}
	}
}

//*********************************************************
//  GR USAGE ONLY	
//  This is used for GR only
// Used to popup the grlogout pages
//*********************************************************

	 popwindow = null;

	 function popUpWindow (popUpType){
		var popUpUrl
			switch(popUpType) {
				case 1:	
					popUpUrl = "/popup/grlogout.aspx";
					break;				
			}
			popwindow = window.open(popUpUrl,"GiftRegistryLogout","resizable=yes,toolbar=no,scrollbars=yes,location=no,directories=no,status=no,menubar=no,width=330,height=260");
	 }

	 function closewindow(){ 
		if(popwindow)
		{ popwindow.close();popwindow=null} 
 		else{popwindow=null}  
		window.location.href = "/gr/Logout.aspx";
	}
//*********************************************************
