//edit:jas descr:form to send an email to the store when user adds item to registry, date:10.JUN.2008
document.write('<FORM NAME="share_newM" METHOD=GET ACTION="survey_new.asp" >');
document.write('<INPUT TYPE=HIDDEN NAME=mscssid VALUE='+ user_guid +'>');
var refmon;
refmon= i_jscript_uu_rootURL+"wish_basket.asp?mscssid=" + user_guid +"&" ;
//document.write('<INPUT TYPE=HIDDEN NAME=referer Value='+ refmon +' >');
document.write('<INPUT TYPE="HIDDEN" NAME="Validate" value="1">');
document.write(' <INPUT TYPE="HIDDEN" NAME="sur" Value=73 >		');
document.write(' <input type="HIDDEN" name="survey_text_field_lab_3" id="survey_text_field_lab_3" value="Wish List Update -- " >');
document.write(' <input type="HIDDEN" name="survey_text_field_3" id="survey_text_field_3" value="Shopper: '+user_firstname+' '+user_lastname+'  " >');
document.write(' <input type="HIDDEN" name="survey_text_field_4" id="survey_text_field_4" value="Email: '+user_email+'  " >');
document.write(" <input type='HIDDEN' name='survey_text_field_5' id='survey_text_field_5' value='' >");
//document.write('<input type="HIDDEN" name="customer_email" value=""> ');
document.write('<input type="HIDDEN" name="share_email" value="service@materialpossessions.com"> ');
document.write('</FORM>');

function go_to_login()
{
	var answer = confirm ("You must be logged into your account to add to your registry. Click OK to Login or Cancel to go back.")
	if (answer)
	{
		visitargs('shopper_lookup.asp','target=dept.asp&dept_id='+dept_id,'');
	}
	else
	{
		return false;
	}
}

function display_product_link(purl,pname,short_desc,price_str,s_id,dept_id,pf_id,basket_enable,porder_code){
	document.write("<a HREF=\"" + purl + "\" class=\"deptlinkP\" target=\"_top\">" + pname + "</a>");
	if(short_desc!="") document.write("<br>"+ short_desc );
	if(porder_code!="" && porder_code!="default" && porder_code!="no_ship") document.write(porder_code+"<br>");
	document.write(price_str);
}

function display_products_gen(Dept_products_gen,p_display_type){ 
	if (p_display_type==null){
		p_display_type=display_type;
	}

	if (Dept_products_gen.length >= 1){
		var sku_index;
		var n_product=0;
		var atn;
		var n_columns=2;
		var index_wide=0;
		atn=0;
		var count = 0;
		
		//addto form
		document.write("<Form name=addtobasket method=GET action=\"shopper_lookup.asp\">");
		document.write("<Input type=hidden name=target value='xt_orderform_additem.asp'>");
		document.write("<Input type=hidden name=auto_reg value='1'>");
		document.write("<Input type=hidden name=sku id=sku value=''>");
		document.write("<Input type=hidden name=qty id=qty value='1'>");
		document.write("<Input type=hidden name=addto id=addtoform value='1'>");
		document.write("<Input type=hidden name=s_id value='" + s_id + "'>");
		document.write("<Input type=hidden name=dept_id id=dept_id value=''>");
		document.write("<Input type=hidden name=pf_id id=pf_id value=''>");
		document.write("</Form>");
		
		//document.write(p_top_HTML);
		//document.write(style_UL_open);
		document.write("<Table border=0 cellpadding=0 cellspacing=0 BGCOLOR=#ffffff width=420><TR><TD>");
		document.write("<Table border=0 cellpadding=1 cellspacing=0 BGCOLOR=#ffffff width=100%>");
		//heading
		document.write("<tr><td class=order_form_header>Product</td><td class=order_form_header>&nbsp;</td><td class=order_form_header>Quantity</td></tr>");
		
		//for each product
		for (atn=0; atn<Dept_products_gen.length; atn++){
			document.write("<tr>");
			document.write("<td class=order_form_td width=40%>");
			
			//name with link, price and more
			display_product_link("product.asp?s_id="+ s_id +"&dept_id="+ dept_id +"&pf_id="+ Dept_products_gen[atn].pf_id +"&"+ ptargetc,Dept_products_gen[atn].name,Dept_products_gen[atn].short_desc,Dept_products_gen[atn].price_str,s_id,Dept_products_gen[atn].dept_id,Dept_products_gen[atn].pf_id,Dept_products_gen[atn].enable_basket,Dept_products_gen[atn].porder_code);
			
			document.write("</td>");
			//my test
			
			//size - color - availability
			document.write("<td class=order_form_td align=right valign=top>");
			
			if(Dept_products_gen[atn].skus.length>0 && Dept_products_gen[atn].skus.length!=1){
				document.write("<Input type=hidden id=\"sku_" +atn+ "\" name=\"sku_" +atn+ "\" value=\"multiple\">");
				document.write("<Select style='font-size:12px;' id=\"sel_" +atn+ "\" name=\"sel_" +atn+ "\">");
				
				
				labelstr="- Select";
				var i=3;
				while (i>0) {
					if(eval("Dept_products[atn].attr_label"+i)!="") labelstr+= " "+eval("Dept_products[atn].attr_label"+i) + " -";
					i--;
				}
				writeoption("",labelstr ,"selected");
				
				
				for (sku_index=0;sku_index<Dept_products_gen[atn].skus.length ;sku_index++){					
					writeoption(Dept_products[atn].skus[sku_index].sku,get_option_val(atn,sku_index),"");						
				}//end for each sku
			}
			else
			{
				document.write(get_option_val(atn,0));
				document.write("<Input type=hidden id=\"sku_" +atn+ "\" name=\"sku_" +atn+ "\" value=\""+Dept_products[atn].skus[0].sku+"\">");
			}
			document.write(" </td>");
			
			//quantity
			document.write("<td class=order_form_td valign=top>");
			if(Dept_products_gen[atn].enable_basket=="1"){
				document.write("<Input type=text size=2 value='0' maxlength=2 name=\"qty_" +atn+ "\" id=\"qty_"+atn + "\">");
				//buy button
				//document.write("&nbsp;<Input type=image onclick=\"return add_all_items(" + (atn+1) + ",'1');\" name=\"addall\" SRC=\"assets/images/btnbuynow.gif\">");
			}else{
				document.write("Not sold online.");
				document.write("<Input type=HIDDEN size=2 value='noadd' maxlength=2 name=\"qty_"+atn + "\" id=\"qty_"+atn + "\">");
			}			
			document.write("</td>");
			
			document.write("</tr>");
			
			document.write("<tr><td colspan=3 class=tablebgcolor height=1><img src=\"assets/images/spacer.gif\" height=1></td></tr>");
			
			count = atn+1;
		}//end for each product
		
		//buttons
		document.write("<tr><td class=tablebgcolor valign=top colspan=3 align=left>");		
		document.write("<table cellspacing=0 cellpadding=0 border=0 width=100%><tr><td align=left>");
		if (user_firstname=="")
		{
			document.write("<Input type=image onclick=\"return(go_to_login())\" alt=\"Add to Your Registry\" src=\"assets/images/btnaddreg.jpg\">");
		}
		else
		{
			document.write("<Input type=image onclick=\"return add_all_items(" + count + ",'4');\" name=\"addall\" src=\"assets/images/btnaddreg.jpg\" alt=\"Add to Your Registry\">");
		}
		document.write(' <a href="javascript:shareproduct(\'share_new.asp\',\'none\',\'SURL\');"><img src="assets/images/btn_email.jpg" alt="Share this page with a friend" class="inputImage"></a>');
		document.write("</td><td align=right>");
		document.write("<Input type=image onclick=\"return add_all_items(" + count + ",'1');\" name=\"addall\" SRC=\"assets/images/btnaddbskt1.gif\">");
		document.write("</td></tr></table>");
		document.write("</td>");
		document.write("</tr>");
		
		document.write("</Table></TD></TR></Table>");
		//document.write(style_UL_close);	
	}else{
		document.write(style_font_1_open + p_no_product_HTML + style_font_1_close);
	}
}

//builds and formats option value for sku dropdown
function get_option_val(atn,sku_index)
{
	var option_val = "";
	if(Dept_products[atn].skus[sku_index].attr_value2!="") option_val += Dept_products[atn].skus[sku_index].attr_value2;
	if(option_val!="") option_val += " - ";
	if(Dept_products[atn].skus[sku_index].attr_value1!="") option_val+= Dept_products[atn].skus[sku_index].attr_value1;				
	if(option_val!="") option_val += " - ";				
	option_val += Dept_products[atn].skus[sku_index].avail_msg;
	//this line entered to insert price when there is a range for the style
	if(option_val!="" && Dept_products[atn].skus.length>1) option_val += " - $"+ OKStrOfPenny(Dept_products[atn].skus[sku_index].price_list);
	return option_val;
}


var number_rows=0;
//useVariantPrice = "True";
//This sorts the product array by attr_value1 then attr_value2 alphabetically only.
function value1_sort(a,b){
	if(a.attr_value1==b.attr_value1){
		if(a.attr_value2 >= b.attr_value2){return 1;}else{return -1;}
	}else{
		if(a.attr_value1 > b.attr_value1){return 1;}else{return -1;}
	}
}

//After the buy button is hit, it goes through all the rows of products and makes the list of skus to add and their quantities.
function qt_validator(){
	var skustr="";
	var qtystr="";

	//number_rows is actually the number of unique styles (not colors) for this product.  This is done on the display section in draw_sku_option2
	for(i=0;i<number_rows;i++){
		//alert(eval("document.all.form1.qty_" + i + ".value") + "-" + eval("document.all.form1.sku_" + i + "[document.all.sku_" + i + ".selectedIndex].value"));
		if(eval("document.all.form1.qty_" + i + ".value") != "0"){
			//we got a value that's NOT zero.  do note that we're not checking for negatives, characters, etc.
			// The ASP on orderform_additem should take care of anything funny.  default add-to is 1.
			if (skustr!=""){skustr=skustr + ","; qtystr=qtystr + ",";}
			skustr=skustr + eval("document.all.form1.sku_" + i + "[document.all.sku_" + i + ".selectedIndex].value");
			qtystr=qtystr + eval("document.all.form1.qty_" + i + ".value");
		}
	}

	//assign the strings we've built to the normal sku and quantity inputs.
	document.all.form1.qty.value=qtystr;
	document.all.form1.sku.value=skustr;
	
	//return true to commit the submit to the next page.  If there are any errors then this is the place to stop it.  return false to stay on the page.
	return true;
}

function dept_add_item(prod_index){
	
	var selsku,elt;
	selsku="";
	//get the selected sku
	elt=document.getElementById("sku_" + prod_index);
	if (elt.value=="multiple"){
		//get the selected index off the dropdown
		elt=document.getElementById("sel_" + prod_index);
		selsku=elt[elt.selectedIndex].value;
	}else{
		selsku=elt.value;
	}
	//alert(Dept_products[prod_index].dept_id + ", " + selsku);
	//return false;
	
	if(selsku!=""){
		//set it to sku value
		document.getElementById("sku").value=selsku;
		document.getElementById("pf_id").value=Dept_products[prod_index].pf_id;
		document.getElementById("dept_id").value=Dept_products[prod_index].dept_id;
		//submit form
		return true;
	}else{return false;}
}

function add_all_items(count,addto){
	var errmsg="";
	var elt;
	var prodname="";
	var skulist="";
	var sku9="";
	var qtystr="";
	var qtyi;
	
	//first check to make sure all items have a chosen sku
	
	
	for (kk=0;kk<count;kk++){
		sku9="";
		if(document.getElementById("sku_" + kk).value=="multiple"){
			elt=document.getElementById("sel_" + kk);
			if(elt){sku9=elt[elt.selectedIndex].value;}
		}else{
			sku9=document.getElementById("sku_" + kk).value;
		}
		if(sku9==''){
			//prodname=document.getElementById("prodname_" + kk).value
			//errmsg=errmsg + "\There was no color picked for " + prodname + ".\n";
		}else{
			qtyi=document.getElementById("qty_" + kk)
  			//set tempvar to qty if possible
  			if(qtyi){tempvar=qtyi.value}else{tempvar=0;}
  			//if qty is valid then add the item
  			if((tempvar != "0") && (IsNumeric(tempvar)) && (tempvar.length>0)){
  				if (skulist!=""){
  				  skulist=skulist + ",";
  				  qtystr=qtystr + ",";
  				}
  				qtystr=qtystr + qtyi.value;
  				skulist=skulist + sku9;
				if(addto==4){document.getElementById("survey_text_field_5").value+="Item: "+sku9+" Quantity: "+qtyi.value+"  ";}
  			}
  		}
	}

	if(skulist.length==0)
		errmsg = "Make the quantity greater than 0 to add a specific product to cart.";
	//if we have all skus properly, submit the add, otherwise give msg and return false.
	if(errmsg.length>0){
		alert(errmsg);
		//return false;
	}else{
		document.getElementById("sku").value=skulist;
		document.getElementById("qty").value=qtystr;
		document.getElementById("addtoform").value=addto;
		if(addto==4){document.share_newM.submit();}
		document.addtobasket.submit();
		//return true;
	}
}

function add_one_of_each(i_prodcount){
	for (kk=0;kk<i_prodcount;kk++){
		if(document.getElementById("qty_" + kk).value != "noadd"){document.getElementById("qty_" + kk).value='1';}
	}
}

function clear_qty(i_prodcount){
	for (kk=0;kk<i_prodcount;kk++){
		if(document.getElementById("qty_" + kk).value != "noadd"){document.getElementById("qty_" + kk).value='0';}
	}
}

//////// HELPER FUNCTION FOR qt_validator //////////////
function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var myChar;

   for (q = 0; q < sText.length && IsNumber == true; q++)
   {
     myChar = sText.charAt(q);
     if (ValidChars.indexOf(myChar) == -1)
     {
       IsNumber = false;
     }
   }
   return IsNumber;
}


function draw_leaf_tree(){
	var base_dept="3001";
	var passed_base=false;
	if ((typeof(leaf_tree)!="nothing")&&(leaf_tree.length>0)){
		document.write("<span id='leaftreeblock'>");
		for (i=leaf_tree.length-1;i>=0;i--){
			if (passed_base && leaf_tree[i].dept_id!="10"  && leaf_tree[i].dept_name!=dept_name){
				//seperator - getting here only after 2 levels deep to created bullet
				document.write("&nbsp;&gt;&nbsp;");
				//link and name
				document.write("<a href=\"" + leaf_tree[i].dept_link + "\" CLASS=\"leaftree\">" + leaf_tree[i].dept_name + "<\/a>");
			}else{

				if (leaf_tree[i].dept_id==base_dept){ //&& leaf_tree[i].dept_name=="Navigation name 10"){
					passed_base=true;
					//HOME link and HOME name
					document.write("<a href=default.asp CLASS=\"leaftree\">HOME<\/a>");
				} //end if after else
			} //end else 
		} // end for
		document.write("</span>");
	}// end if typeOf
} // end function

function shareproduct(PageURL,args,SURL) { 
	var plink;
	var emailbody;
	var url;
	if (args.indexOf("=")<0 ||args.indexOf("&")<0 ) {
		args="";
	}
	
	PageURL =PageURL + '?';
	
	plink=i_jscript_uu_rootURL+"dept.asp?dept_id=" + dept_id +"&ad_id=share_email" ;
	emailbody="Dear #RecipientName#,<BR> #Text_field_3# <BR><BR>";
	
	emailbody = emailbody + "<a href=\""+plink+"\" >"+dept_name+"</a><br>" ;
			
	//emailbody = emailbody + "<a href=\""+plink+"\" >"+ product_name + "</a><BR>" ;
	emailbody = emailbody + "from #FromName#  at <A href=\"mailto:#FromEmail#\">#FromEmail# <BR>" ;
	
	window.location = PageURL +"referer="+escape(plink)+"&email_body="+escape(emailbody)+"&target="+ escape(window.location.href)+"&";
	
	//Note: This return is required as well
	// false tells the browser to ignore the default action entirely
	//usage Click Here II
	 return(false);
} //end shareproduct
