/*

Copyright eWish.com  2010. All rights reserved.

*/

function CollectGiftProperties(storeID, storeURL)	//atr button has to been placed in the same form
{
	//alert("In JS CollectGiftProperties");
	
	
	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("lblName"))
	{
// 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("lblSKU"))
	{
// 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-----------------*/
/* There may be more than one SELECT remove the ==1 so scrip will grab if there are more than on SELECTS.
 This now says look for a SELECT that is in in Form1 and grab the visitors choice.  
 Of course there should only be product related options within Form1 */

//if((document.getElementsByTagName("select").length) ==1) Replaced with below - eWish 08/09/2010
if(document.getElementsByTagName("select"))
	{
// yes exists call function to get value
	giftOptions = getOptions();
	sendStr += " Size" + '=' + escape(giftOptions);
	//alert(sendStr);
	
	} else{
					
			giftOptions = "No Options Found";
			sendStr += " Size" + '=' + escape(giftOptions);
		}
	
/* -----------------------end call to get options -----------------*/	
	



/* -----------------------Try to get picture number: eproductimage----------------*/	
//Get image source from img id=MainImage OR whatever name you used to ID the image. If MainImage does not exist use the page URL.
	if (document.getElementById("MainImage"))
	{	
	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("lblSitePrice"))
		{
		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("lblDescription")){	
		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);
		}
		
		
	
	
// Same for options if merchant has options they will be in field attribsOptionsText
		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("lblDescription").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="MainImage" else make it the page url.
function getGiftPicSource()
{
	var rtnString ="";
	var itemDisplayedHTML="";
	
	itemDisplayedHTML = document.getElementById("MainImage").src;
	rtnString =trim(itemDisplayedHTML);
	return rtnString;
			
	}


/* function to pull price out of element productPrices */
function getPriceFromHTML(itemPriceHTML)
{
	var rtnString ="";
	var itemPriceHTML="";

	
	itemPriceHTML = document.getElementById("lblSitePrice").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("lblName").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="lblSKU*/
	
function getModel()
{
	var rtnString ="";
	var itemDisplayedHTML="";
	

	itemDisplayedHTML = document.getElementById("lblSKU").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["Form1"];
	//alert("search option");
		
	with(formobj){
	for (i=0; i< length; i++){
						
				//get options; there may be more than one option selects
				if (elements[i].type == "select-one") {
				  
						m = elements[i].selectedIndex;
					
					if (m > -1) {
						
						if (elements[i].options[m].text.toUpperCase().indexOf("-Select a Design-")!= 0){	//do not send options starting with "Choose ...", for example, don't send "Choose a Color"
						
						giftOption += elements[i].options[m].text + ' - ';
						}
														
					}
									
				}
			
			}
	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*$/, '');
    }
	