<!--	
/*  02-12-07 This is a modified version of original eWish MC code.  
It is designed for those stores with a more dyamic Title field

This new code will do this:
MC titles sections often break down to categories, them product name, then product id all seperated  with a "-" like:
<title>Crib Bedding by Brand - Cotton Tale Designs - Dolly by Cotton Tale Design - DL4S</title>
Need to get the last two sections; the very last is the product id and the second to last is product name.
Build an array, each section divdied by "-" is an element, grab the last two elements, the very last is the
product id the next to last is the gift name.

Normally Prod ID appears in these MC fields: strProductPartNumberBase or strProductMFGPartNumberBase, if 
Prod ID is not in those fields then use above method and grab last section of Title

*/

function CollectGiftPropertiesMC(storeID, formobj)	//atr button has to been placed in the same form
{
	
	var giftModel = "";
	var giftName = "";
	var giftDescript = "";
	var giftImgSrc = "";
	var giftPrice = "";
	var giftQuantity = "";
	var giftOption = "";
	var sendStr = "";
	var modelidfound =0;
	
	//send storeid
	sendStr += "storeid" + '=' + storeID;
		
	if (formobj)	
	{
	//do nothing
	}
	else	//formobj not found (atr button is outside the form), hard code it to frmAddItemToCart form
	{
		formobj = document.forms["frmAddItemToCart"];
		
	}

	if (formobj.name !="frmAddItemToCart")		//more than one form, get gift name from link?
	{
		giftName = getGiftNameFromLink(formobj);	
		 
	}
	else	//single gift screen, get gift name from title
	{
		giftName = getGiftNameFromTitle();
		
	}
	
	if (giftName != ""){
		sendStr += "&" + "gname" + '=' + escape(giftName);
		giftDescript = giftName;	//will use later for description
		
	}


	//get image source from <img id=idMainImg>
	if (document.getElementById("idMainImg")){	
		giftImgSrc = document.getElementById("idMainImg").src;

		sendStr += "&" + "gdescURL" + '=' + escape(giftImgSrc);
		
	}

	//mc form name is always frmAddItemToCart
	//formobj = document.forms["frmAddItemToCart"];
	//formobj = this.form;	
	with(formobj){
				
		for (i=0; i< length; i++){
			//get price
			if (elements[i].name == "ProdPrice") {
				giftPrice = elements[i].value;

				sendStr += "&" + "price" + '=' + escape(giftPrice);
		
			}
			//get quantity
			if (elements[i].name == "intQty") {
				giftQuantity = elements[i].value;

				sendStr += "&" + "Quantity" + '=' + escape(giftQuantity);
			
			
			}
			//get MC product number for eWish model field
			if (elements[i].id == "strProductMFGPartNumberBase") {
				 modelidfound=1;
				giftModel = elements[i].value;
				sendStr += "&" + "model" + '=' + escape(giftModel);
						
			}
			//get MC product number for eWish model field
			// 02-12-2007 some MC store use strProductPartNumberBase check to see.
			if (elements[i].id == "strProductPartNumberBase") {
				modelidfound=1;
				giftModel = elements[i].value;
				sendStr += "&" + "model" + '=' + escape(giftModel);
						
			}
			
			
			
			//get options; there may be more than one option selects
			if (elements[i].name == "VariantProductOption_ID") {
				m = elements[i].selectedIndex;
				if (m > -1) {
					
					if (elements[i].options[m].text.toUpperCase().indexOf("NO ")!= 0){	//do not send options starting with "NO ...", for example, don't send "NO comfort selected"
					giftOption += elements[i].options[m].text + ' - ';
					}
								
				}
				
			}

			//get option in objects whose names start with "PQ-" 
			if (elements[i].name.indexOf("PQ-") == 0) {
				giftOption += elements[i].value + ' - ';	
				
			}


			
		}
	}
	
	// 02-12-07 product id was not found using conventional MC identifiers, us info found in page titel <titel></title>
	if (modelidfound !=1)	{
		giftModel = getProdIDFromTitle();
		sendStr += "&" + "model" + '=' + escape(giftModel);
		
		}

	giftOption = giftOption.substring(0, giftOption.length - 3); //take out last ' - ' from option text
	
	// making eWish description that gift giver will see on gift list and user will see on confirm popup;
	// some MC store use very long title so use that and the selected options if any
	if (giftOption !="")
	{
		sendStr += "&" + "gdesc" + '=' +escape(giftDescript)+" "+escape(giftOption);
		//alert("options NOT blank: "+sendStr);
	} else { 
	sendStr += "&" + "gdesc" + '=' + escape(giftDescript);
	//alert("options blank: "+sendStr);
	}
		 
	
			
	//send option if giftOption is not empty
	// Peter's note:  I added giftName to string.  eWish currently uses gift name + options to make model type
	if (giftOption !="")
	{sendStr += "&" + "EwishP" + '='+"option="+escape(giftOption);}
	//alert("EwishP" + '='+"option="+escape(giftOption))
	//alert(sendStr);
	PopUpOpenMC('http://www.ewish.com/merchants/atr.cfm',"EwishLogon", sendStr);
	
	
	
	
}
//end of function CollectGiftPropertiesMC
	
	
	function PopUpOpenMC(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();
	}



/*  02-12-2007 if registrant does not land on product page directy title will include category which
is too long.  Need to work back from right to left in title.  */

function getGiftNameFromTitle()
{
	var rtnString ="";
	
	if (document.title){	
		rtnString = document.title;
		
		var how_long = rtnString.length; 
		if (how_long > 25){
			//rtnString= rtnString.substring(0,25);
			
			/*02-12-2007 MC stores are splitting title sections with a "-" like
			<title>Crib Bedding by Brand - Cotton Tale Designs - Dolly by Cotton Tale Design - DL4S</title>
			Need to get the last two sections; the very last is the product id and the second to last is product name.
			Build an array, each section divdied by "-" is an element, grab the last two elements, the very last is the
			product id the next to last is the gift name.
			*/
			var title_array=rtnString.split(" - ");
			var titlesection_num=0;
			var total_titlesections = title_array.length;
			var giftnane_section = (total_titlesections -2);
			
			rtnString=title_array[giftnane_section];
				
						
			}
						
	}
	return rtnString;
}


// function to get product id from title and use as ewish model
	/*02-12-2007 MC stores are splitting title sections with a "-" like
	<title>Crib Bedding by Brand - Cotton Tale Designs - Dolly by Cotton Tale Design - DL4S</title>
	The very last is the product id, build an array, each section divdied by "-" 
	is an element, grab the very last and make it the model number.
	*/
function getProdIDFromTitle()
{
	var rtnString ="";
	
	if (document.title){	
		rtnString = document.title;
			var title_array=rtnString.split(" - ");
			var total_titlesections = title_array.length;
			var prodid_section = (total_titlesections -1);
			rtnString=title_array[prodid_section];
	}
									
	return rtnString;
}


function getGiftNameFromLink(formobj)
{
	var rtnString ="";
	
	if (formobj.ProdID){
		var prodID = formobj.ProdID.value;
		var linkQuery = "ProdID=" + prodID;
		//now search for href that ends with "ProdID=xxx", xxx is prodID
		//document.links
		for (i=0; i< document.links.length; i++){
			
			if (document.links[i].href.indexOf(linkQuery)==document.links[i].href.length - linkQuery.length)
			{
				
				if (document.links[i].innerText) { // IE; 
					rtnString = document.links[i].innerText; 
				} 
				else{ 
					if (document.links[i].textContent) 	
					{ 
						rtnString = document.links[i].textContent; 
					} 

				}
				
			}
		
		
		}
	
	}
	//alert(rtnString);
	return rtnString;
}



//end of wei's test function


//-->

