/*

Copyright eWish.com  2010. All rights reserved.

*/
var Send=1;
var radsel=0;  //set to 1 when a checked radio button is found
var radbutton=0; //set to 1 when a radio button type is found

function CollectGiftPropertiesBig(storeID, storeURL)	//atr button has to been placed in the same form
{
	
	//alert("entering ewish");
	
	var giftModel = "";
	var giftName = "";
	var giftDescript = "";
	var giftImgSrc = "";
	var giftPrice = "";
	var giftQuantity = "";
	var giftOption = "";
	var sendStr = "";
	var modelidfound =0;
	var formobj ="";
	var merchantSite="";
	
	
	
	//send storeid
	sendStr += "storeid" + '=' + storeID;
	
	
/* -----------------------Try to get model number: lblName-----------------*/		
// Check if name element id lblName exists
	if(document.getElementById("eproductname"))
	{
// yes exists call function to get value
	giftName = getGiftName();
	sendStr += "&" + "gname" + '=' + escape(giftName);
	//alert(sendStr);
	} else{
		
			giftName = "Name ID Not Found";
			sendStr += "&" + "gname" + '=' + escape(giftName);
		}

	

/* -----------------------Try to get model number of SKU or whatever:  esku-----------------*/	
	
// Check if  element id esku exists or whatever id you gave to the sku number.  If no SKU or model consider using the name 
	if(document.getElementById("esku"))
	{
// yes exists call function to get value
	giftModel = getModel();
	//alert(giftModel);
	sendStr += "&" + "model" + '=' + escape(giftModel);
	//alert(sendStr);
	
	} else{
		
			//giftName = getGiftName();
			//sendStr += "&" + "model" + '=' + escape(giftName);
			giftModel = "Model ID Not Found";
			sendStr += "&" + "model" + '=' + escape(giftModel);
		}
	
	
	
/* -----------------------try to get options to append to model field-----------------*/


if(document.getElementById("eoptions"))
	{
// yes exists call function to get value
	//alert("Ready to get options");
	giftOptions = getOptions();
	sendStr += " Size" + '=' + escape(giftOptions);
	//alert(sendStr);
	
	} else{
					
			giftOptions = "No Options Found";
			sendStr += " Size" + '=' + escape(giftOptions);
			//alert(sendStr);
		}
	
/* -----------------------end call to get options -----------------*/	
	



/* -----------------------Try to get picture number: eproductimage----------------*/	
//Get image source from img id=eproductimage OR whatever name you used to ID the image. If MainImage does not exist use the page URL.
	if (document.getElementById("eproductimage"))
	{	
	giftImgSrc = getGiftPicSource();
	sendStr += "&" + "gdescURL" + '=' + escape(giftImgSrc);
	//alert(sendStr);
	} else{
		
			giftImgSrc = document.location.href;
			sendStr += "&" + "gdescURL" + '=' + escape(giftImgSrc);
		}

	
	

	
		
/* -----------------------Try to get price: lblSitePrice-----------------*/	
// Check if name element id eprice exists
	 if (document.getElementById("eprice"))
		{
		giftPrice =	getPriceFromHTML();
		sendStr += "&" + "price" + '=' + escape(giftPrice);
		//alert(giftPrice);
		}
 		else{
		
		/* if gift price section not found setting price to 0 which should cause 
		ewish warning to appear to user to about not enough info found to add gift. */
		giftPrice=0;
		sendStr += "&" + "price" + '=' + escape(giftPrice);
		}
	
/* -----------------------Try to get description: lblDescription-----------------*/	
// Check if name element id edescription exists
	if (document.getElementById("edescription")){	
		giftDescript =getDescriptFromHTML()
		sendStr += "&" + "gdesc" + '=' +escape(giftDescript);
		//alert(giftDescript);
	} else{
		
		/* if descript not found make it same as product name */
		giftDescript=giftName();
		sendStr += "&" + "gdesc" + '=' + escape(giftDescript);
		}
		
		
	//alert('SendStr');
	
// Same for options if merchant has options they will be in field attribsOptionsText
		//alert("Just before calling popup Send is "+Send);
		if (radbutton == 1 && radsel == 0){
		alert("Please select an option before sending to Gift Registry");
		Send = 0;
		}
		
		if (Send == 1){
		PopUpOpen('http://www.ewish.com/merchants/atr.cfm',"EwishLogon", sendStr);}
		
} // end function CollectGiftInfo, now open pop-up


function PopUpOpen(url,winname, queryString){
		winprops = ",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1";
		//ATRpopup = window.open(url,name,"height=550,width=380"+winprops);
		ATRpopup = window.open(url+ "?"+queryString,winname,"height=550,width=380"+winprops);
		ATRpopup.focus();
	}



/* item description appears to be within span id of lblDescription	*/
function getDescriptFromHTML()
{
	var rtnString ="";
	var itemDisplayedHTML="";

	itemDisplayedHTML = document.getElementById("edescription").innerHTML.replace(/^\s+|\t+|\n+|\'|\"|\?|\!|\s+$/g, '');
	itemDisplayedHTML = itemDisplayedHTML.slice(0,100);
	//alert("description_:"+itemDisplayedHTML);
	if(itemDisplayedHTML.length == 0)
		{
		//alert("blank description");
		
		rtnString ="Description area blank";
		
		//if no description consider calling get gift name funtion getGiftName()
		//getGiftName();
		return rtnString;
		}
	
	rtnString =trim(itemDisplayedHTML);
	return rtnString;
			
	}

// Function to get picture URL image is in table id="lnkMainImg else make it the page url.
function getGiftPicSource()
{
	var rtnString ="";
	var itemDisplayedHTML="";
	
	itemDisplayedHTML = document.getElementById("eproductimage").src;
	rtnString =trim(itemDisplayedHTML);
	return rtnString;
			
	}


/* function to pull price out of element productPrices */
function getPriceFromHTML(itemPriceHTML)
{
	var rtnString ="";
	var itemPriceHTML="";

	
	itemPriceHTML = document.getElementById("eprice").innerHTML;
	//alert (trim(itemPriceHTML));
	rtnString =trim(itemPriceHTML);
	return rtnString;
  
}




/* item name is within element id lblName */
function getGiftName()
{
	var rtnString ="";
	var itemDisplayedHTML="";
	
	itemDisplayedHTML = document.getElementById("eproductname").innerHTML;
	itemDisplayedHTML=trim(itemDisplayedHTML);
	//if inner HTML was blank set to space
	//if(itemDisplayedHTML.value == "")
	if(itemDisplayedHTML.length == 0)
		{
		rtnString ="Product Name area blank";
		return rtnString;
		}
	//trim it and pass it back
	rtnString =trim(itemDisplayedHTML);
	return rtnString;
			
	}


/* model number lies between span id="esku*/
	
function getModel()
{
	var rtnString ="";
	var itemDisplayedHTML="";
	

	itemDisplayedHTML = document.getElementById("esku").innerHTML;
	itemDisplayedHTML=trim(itemDisplayedHTML);
	
	//if inner HTML was blank set to space
		
		//if(itemDisplayedHTML.value == null)
		if(itemDisplayedHTML.length == 0)
		{
		itemDisplayedHTML ="NoSKUorModel";
		rtnString =trim(itemDisplayedHTML);
		return rtnString;
		}
	// the inner html was found, trim it and pass it back
		
	rtnString =trim(itemDisplayedHTML);
	return rtnString;
	
		
	}

// Function to get options, if any. Should only get here if the element id eoption was found (or whatever you used to id variations area.)
function getOptions()
{
	
	var rtnString ="";
	var giftOption ="";
	formobj = document.forms["productDetailsAddToCartForm"];
	
	with(formobj){
	for (i=0; i< length; i++){
						
				//get options; there may be more than one option selects
				//if (elements[i].type == "select-one") {
				  if (elements[i].name.indexOf("attribute") == 0) {
					  			  
						// is it radio?
						 
						 
						 if  (elements[i].type.indexOf('radio') != -1){
							 //yep it's a radio select
							 //alert("Radio Option");	
								radbutton = 1;
							if (elements[i].checked) {
								var rad_value = elements[i].value; //find out value of radio it is unique so use it to help identify
								giftOption=  getRadioDisplay(rad_value); //pass value to funtion. Function will pull out HTML that visitor sees near radio.
								
								rtnString =trim(giftOption); //return the found HTML
								radsel = 1;
								Send = 1;
								}
							}
							  
					 // if not radio it must be a select box
					m = elements[i].selectedIndex;
					//alert("Select Option");	
					//alert("m= "+m);
					
					if (m > -1) {
						if (elements[i].options[m].text.toUpperCase().indexOf("-- PLEASE")!= 0){	//do not send options starting with "Choose ...", for example, don't send "Choose a Color"
						giftOption += elements[i].options[m].text ;
						Send = 1;
						//alert(Send);
						}	
					else { alert("Please choose an option before sending to Gift Registry");
							Send = 0;
							//alert(Send);
							}
					}									
				}			
			}
	rtnString =trim(giftOption);
	//alert("options: "+rtnString);
	
	return rtnString;
	}
}

	
// Hmmm... found this on the web to help trim
function trim(str) 
	{  
       
	//  return str.replace(/^\s+|\s+$/g,"");
		return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    }
	
	
	
	
/*  This function will attempt to pull out the text a visitor sees near radio button.
We notice that each radio button is an input type with a unique value. The the input closes >, then 
text for example a Size, then a closing label tag.  
Function will find the index of closing input tag (>), and the index of bracket (<) that starts closing label tag. 
Then grab the text in between the two.  If label tags are not used this script will need to be modified.
*/
function getRadioDisplay(rad_value){
	
	var rtnString = "";	
	var iStartIndex, iEndIndex, strRemaining;
	strRemaining = "";
	var searchStringBegin = 'value="'+ rad_value +'"';
	//alert("searchStringBegin_"+searchStringBegin);
	var searchStringEnd = '</label>';
	var sourceString = document.body.innerHTML;

	
	iStartIndex = sourceString.indexOf(searchStringBegin);	//looking for searching string
	//alert("iStartIndex_"+iStartIndex);
	if (iStartIndex > -1) {
		iStartIndex +=  searchStringBegin.length;	//get start position of return string
		iStartIndex = iStartIndex +1; //get passed the last > in HTML like > 2<
		//alert("iStartIndexLENGTH_"+iStartIndex);
		strRemaining = sourceString.substring(iStartIndex, sourceString.length);	//get the text between the first found to the end of body tag	
		//alert(strRemaining);
		iEndIndex = strRemaining.indexOf(searchStringEnd);
		rtnString = strRemaining.substring(13, iEndIndex);
	}
	return rtnString;


}


