// Ajax Javascript Functions
function getCurrentPage(){
	var page = "";
	page=window.location.pathname.toString().replace("checkout/","");
	page=page.replace("/","");
	page=page.replace(".php","");
	var pattern = /(P\/|detail)/i
  if(pattern.test(page) && page.charAt(page.length-1) == "/"){ page = "detail";}
  else if(page.charAt(page.length-1) == "/"){page="search";}
	return page;
}
var pageForm="",page=getCurrentPage();
var handleSuccess = function callBack(o) {
	//alert(o.responseText);
	var detailContent = document.getElementById('detailrequest');
	detailContent.innerHTML = o.responseText; 
	document.getElementById("BVCustomerRatings").innerHTML = document.getElementById("BVsummaryHold").innerHTML;
	document.getElementById("hideBV").innerHTML = document.getElementById("BVreviewHold").innerHTML;
	if (reviews > 0) {
  document.getElementById("hideBV").style.display = "block";
  }else{
  document.getElementById("hideBV").style.display = "none";
  document.getElementById('BVNoReviews').style.display = "block";
  }
} 
var handleFailure = function callBack(o) {
	//alert(o.argument);
	var newURL = 'detail.php?parentId='+o.argument[0];
	if (o.argument[1]!=''){
		newURL += '&spec='+o.argument[1];
	}
	if (o.argument[2]!=''){
		newURL += '&displayPage='+o.argument[2];
	}
	alert(newURL);
	//window.location(newURL);
}
var callback = { success:handleSuccess, 
								 failure:handleFailure
	}
function sendform(target,formName) {
   YAHOO.util.Connect.setForm(formName);
   YAHOO.util.Connect.asyncRequest('POST',target,callback);
}
function swatchClick(spec,item,size){
	//alert("SWATCH IT BUDDY");
	var sendUrl = 'item='+item+'&spec='+spec+'&size='+size;
	callback.argument=[item,spec,''];
	YAHOO.util.Connect.asyncRequest('POST','detailRequest.php',callback,sendUrl);
}
function dropDownChange(item,size,spec){
	var sendUrl = 'item='+item+'&size='+size+'&spec='+spec;
//	alert(sendUrl);
	callback.argument=[item,spec,''];
	YAHOO.util.Connect.asyncRequest('POST','detailRequest.php',callback,sendUrl);
}
// Soft does not currently have tabs on detail page.
function tabClick(item,spec,displayPage){
	var sendUrl = 'item='+item+'&spec='+spec+'&tabPage='+displayPage;
	//alert(sendUrl);
	callback.argument=[item,spec,displayPage];
	YAHOO.util.Connect.asyncRequest('POST','detailRequest.php',callback,sendUrl);
}
function clone(form){
	var Input,y,add;
		newShip= document.createElement("FORM");
		newShip.method = "POST";
 		newShip.name = "newShip";
 		newShip.action = form.action;
 		for(x=0; x<form.length;x++){//loop through each form object
 			add=true;
 			if(form.elements[x].type=="radio" && !form.elements[x].checked){add=false;}
 			if(add){
 				try{  Input = "<input name='"+form.elements[x].name+"' type='hidden' value='"+form.elements[x].value+"'>";
							y = document.createElement(Input);
				}catch(E){
					y = document.createElement("input");
					y.setAttribute("type", "hidden");
 	 				y.setAttribute("name",form.elements[x].name);
  				y.setAttribute("value",form.elements[x].value);
  			}
				newShip.appendChild(y);
			}
		}
		return newShip;
}
var processUPS = function UPScallBack(o){
	var parentDiv = document.getElementById('accountManager'),check = o.responseText.substr(6,5),
	today = new Date(),month=parseInt(today.getUTCMonth())+1,AvCode=1,template,pageBody,form1;
	if(check =="valid" || check=="no ca" || check=="preva"){
		if(check=="no ca"){AvCode=4;}
		pageForm = document.shippingAddress;
		if(pageForm===undefined){pageForm = document.shipToForm;}
		if(pageForm===undefined){pageForm = document.billToForm;}
		if(check!="preva"){
			if(pageForm.AvCode==undefined&&pageForm.AvDate==undefined){ //already validated
				addElement({'tag':'input','parent':pageForm,'attributes':[{'name':'type','value':'hidden'},
						 	{'name':'value','value':AvCode},{'name':'name','value':'AvCode'}]});
				addElement({'tag':'input','parent':pageForm,'attributes':[{'name':'type','value':'hidden'},
						 	{'name':'value','value':today.getUTCFullYear()+"-"+month+"-"+today.getUTCDate()},
						 	{'name':'name','value':'AvDate'}]});
			}
		}
		if(pageForm.nextPage!=undefined){
			if(page != "billTo"){pageForm.nextPage.value=0;}
			else{pageForm.nextPage.value=1;}
		}
		pageForm.submit();	
		}
	else{
		
		if(parentDiv==undefined){
			parentDiv = document.getElementById('checkoutBody');
			parentDiv.innerHTML = "";
			template = document.getElementById('checkoutTemplate');
			pageBody = document.getElementById('pagebody');
			form1 = document.shipToForm;
			if(form1==undefined){form1=document.billToForm;}
			pageBody.removeChild(form1);
			pageBody.appendChild(template);
		}
		parentDiv.innerHTML = o.responseText;
		}
}
var UPScallback = { success:processUPS, 
								 		failure:handleFailure
}
function upsVal(form){
	var sendUrl = loadUpPars(form);
	UPScallback.argument=[form];
	pageForm = clone(form);
	YAHOO.util.Connect.asyncRequest('POST','/ups.php',UPScallback,sendUrl);
}
function loadUpPars(itemForm){
	var pars = "";var add= true;
	for(x=0;x<itemForm.length;x++){
	add=true;if(itemForm.elements[x].name == "radio" && !itemForm.elements[x].checked){add=false;}
	if(add){if(x!=0){pars+="&";} pars+=itemForm.elements[x].name+"="+itemForm.elements[x].value; }}
	pars = pars.replace(/\?/g,'*.*');
	return pars;
}
function addElement(domE){
		var E = document.createElement(domE.tag),attr,i;
		numAtts=domE.attributes.length;
		for(i=0;i<numAtts;i++){
		  attr = domE.attributes[i];
			E.setAttribute(attr.name,attr.value);
		}
		domE.parent.appendChild(E);	
}
function pXAVchoice(selected){
	AvCode=3;
	var parentDiv = document.getElementById('accountManager'),today = new Date(),month =parseInt(today.getUTCMonth())+1,
	inputName,Input,address;
	if(parentDiv==undefined){parentDiv = document.getElementById('checkoutBody');}
	if(selected){
		AvCode=2;
		inputName="addr"+document.posAddrs.addrSelect.selectedIndex;
		Input = document.posAddrs.elements[inputName].value;
  	address = Input.split("|");
  	if(address.length>4){pageForm.apt.value = address[1];}
  	pageForm.streetAddress.value = address[0];
  	pageForm.city.value = address[1];
  	pageForm.zip.value = address[3];
  	pageForm.state.value = address[2].toUpperCase();
  }
	addElement({'tag':'input','parent':pageForm,'attributes':[{'name':'type','value':'hidden'},
				{'name':'value','value':AvCode},{'name':'name','value':'AvCode'}]});
	addElement({'tag':'input','parent':pageForm,'attributes':[{'name':'type','value':'hidden'},
				{'name':'value','value':today.getUTCFullYear()+"-"+month+"-"+today.getUTCDate()},
				{'name':'name','value':'AvDate'}]});
	if(pageForm.nextPage!=undefined){
		if(page != "billTo"){pageForm.nextPage.value=0;}
		else{pageForm.nextPage.value=1;}
	}
	parentDiv.style.visibility = "hidden";
  parentDiv.appendChild(pageForm);
  pageForm.submit();
}

function getSelectedSortBy(){
	var str="",sorts=document.getElementById('sorts');
	if(sorts!=undefined){
		str=sorts.options[sorts.selectedIndex].value;
	}
	return str;
}