﻿/* HTML.js,Payment.js must be included */

for (var i=0;i<fields.length;i++){
  if (fields[i].get('value')!=null)
   if ( fields[i].get('value').indexOf("##")==0 ) fields[i].set('value',"");
}

if (typeof(undefined) != typeof(shipp_fields)){
	for (var i=0;i<shipp_fields.length;i++)
	  if (shipp_fields[i].get('value')!=null)
	    if ( shipp_fields[i].get('value').indexOf("##")==0 ) shipp_fields[i].set('value',"");
}

//----------------------------------------------------
function showCViewForm(){
 for (var i=0;i<PAYMENT_LIST.size();i++ )
	Payment.addPayType( (PAYMENT_LIST.get(i)).getType() );

  if (checkoutStage==1){
		ShoppingCart.cart.show(ShoppingCart.OP_ONLY_SUBTOTAL); 
		document.write('<table border="0" width="96%" class="checkoutbuttonstable"><tr>');
		document.write('<td align="'+Visual.left+'"><input class="RButChOut01" type="button" value="'+Visual.strRemoveSelected+'" onclick="ShoppingCart.refresh()"></td>');
		document.write('<td align="center"><input class="RButChOut01" type="button" value="'+Visual.strContinueShopping+'" onclick="back()"></td>');
		document.write('<td align="'+Visual.right+'"><input class="RButChOut01" type="button" value="'+Visual.strCheckout+'" onclick="checkout(2)"></td>');
		document.write('</tr></table>');
 }else if (checkoutStage==2){
		ShoppingCart.cart.show(ShoppingCart.OP_ONLY_SUBTOTAL|ShoppingCart.OP_DIS_CART); 
		document.write('<input class="RButChOut01" type="button" value="'+Visual.strContinueShopping+'" onclick="back()">');
 }else if (checkoutStage==3){
		ShoppingCart.cart.show(ShoppingCart.OP_DIS_CART); 
		document.write('<input class="RButChOut01" type="button" value="'+Visual.strContinueShopping+'" onclick="back()">');
 }

 document.write('<form name="fcheckout" method="post">');
 document.write('<input type="hidden" name="lang"><input type="hidden" name="mailprod">');
 document.write('<input type="hidden" name="cpage"><input type="hidden" name="count">');
 document.write('<input type="hidden" name="pids"><input type="hidden" name="prophash">');
 document.write('<input type="hidden" name="payment"><input type="hidden" name="paymenttypes">');
 document.write('<input type="hidden" name="ref"><input type="hidden" name="charge_ids">');
 document.write('<input type="hidden" name="total"><input type="hidden" name="checkoutstage">');
 document.write('<input type=hidden name="e_mailpass"><input type=hidden name="passpass" >');

 if (checkoutStage==1){
		ShoppingCart.setValues(true);
 }else if (checkoutStage==2){
		ShoppingCart.setValues(true);
		showPayFormStr(Visual.strBInfo,Visual.strSInfo,Visual.strSameAsB,false);
		document.write('<br><br><input class="RButChOut01" type="button" value="'+Visual.strContCheckout+'" onclick="checkout(3)">');
 }else if (checkoutStage==3){
		ShoppingCart.setValues(false);
		showPayFormStr(Visual.strBInfo,Visual.strSInfo,Visual.strSameAsB,true);
		document.write('<table cellspacing="0" cellpadding="0" class="persdet" >');
		document.write('<tr>');
		document.write('<td class="persdetl">'+Visual.strPayMethod+'</td> <td class="persdetr"> ');
		Payment.showTypesInSelect();
		document.write('</td>');
		document.write('</tr>');
		document.write(' </table>');
 }
 document.write('</form>');	
 if (checkoutStage==3){
	document.write('<form name="hform">');
	 Payment.showTypesTables(PAYMENT_LIST);
	document.write('</form>');
	document.write('<input class="RButChOut01" type="button" value="'+Visual.strSendOrder+'" onclick="checkout(4)">');
 }

 if (checkoutStage>1 && isCountryVisible()){
	 
  SelectTag.selectOptionByValue(document.fcheckout.elements['countryID'],selectedCountryOption);
  setZonesByCountry(document.fcheckout,false);
  SelectTag.selectOptionByValue(document.fcheckout.elements['stateID'],selectedStateOption);
  if (Project.isShippingReq==1){
	  SelectTag.selectOptionByValue(document.fcheckout.elements['shipp_countryID'],shippSelectedCountryOption);
  	  setZonesByCountry(document.fcheckout,true);
	  SelectTag.selectOptionByValue(document.fcheckout.elements['shipp_stateID'],shipSelectedStateOption);
  }
 }

/* //set search from fields
 if (srchform!=null) {
	SelectTag.selectOptionByValue(srchform.elements['sby'],Url.getCurrentValue('sby'));
	srchform.elements['minprice'].value=Project.srch_minprice;
	srchform.elements['maxprice'].value=Project.srch_maxprice;
	srchform.elements['srch'].value=Project.srch_srch;
 }*/

}

function showPayFormStr(billInfoStr,shippInfoStr,sameAsStr,isReadOnly){

document.write('<table><tr><td><div id="integr_button">');
if (Project.Integrate!=Integrate.NOINTEGRATION && ShoppingCart.total!=0)
	document.write(Integrate.getButtonStr(Project.Integrate));
document.write('</div></td></tr></table>');

document.write('<table cellspacing="0" cellpadding="0" class="persdet" >');
document.write('<tr><td colspan=2><b>'+billInfoStr+'</b></td></tr>');
for (var i=0;i<fields.length;i++){
	if (isReadOnly)fields[i].add('disabled','disabled');
	showDataLine(fields[i],fdescr[fields[i].get('name')]);
}
document.write('</table>');   

if (Project.isShippingReq==1){
	document.write('<table cellspacing="0" cellpadding="0" class="persdet" >');
	document.write('<tr><td colspan=2><b>'+shippInfoStr+'</b>');
    if (!isReadOnly)
		document.write('<input checked name="isshipping" id="isshipping" type="checkbox" value="1" onclick="this.value=1-Number(this.value);Html.displayElement(\'shippinginfodiv\',1-Number(this.value));"> '+sameAsStr);
	else{
		document.write('<input name="isshipping" id="isshipping" type="hidden" value="'+isshippingOption+'">');
	    if (isshippingOption==1) document.write(sameAsStr);
	}

	document.write('</td></tr>');
	document.write('<td colspan=2>');
	if (isshippingOption==1 || document.fcheckout.elements['isshipping'].checked ){
			document.write('<div id="shippinginfodiv" style="display: none;">');
			document.fcheckout.elements['isshipping'].value=1;
	}else{
			document.write('<div id="shippinginfodiv" style="display: block;">');
			document.fcheckout.elements['isshipping'].value=0;
	}
	document.write('<table cellspacing="0" cellpadding="0" class="persdet" >');
	for (var i=0;i<shipp_fields.length;i++){
		if (isReadOnly)shipp_fields[i].add('disabled','disabled');
		var tname=shipp_fields[i].get('name').substr(6);
		showDataLine(shipp_fields[i],fdescr[ tname ] ); 
	}
	document.write('</table>');
	document.write('</div>');
	document.write('</table>');
 }

}

function setZonesByCountry(form,isShipp){
	if (isShipp){
	  var countrySel=form.elements['shipp_countryID'];
	  var sel=form.elements['shipp_stateID'];
  }else{
	  var countrySel=form.elements['countryID'];
	  var sel=form.elements['stateID'];
  }
  var countryId=SelectTag.seletedValue(countrySel);

  SelectTag.removeAllOptions(sel);
  if (ZONE_LIST[countryId]){
	  for (var i=0;i<ZONE_LIST[countryId].length;i+=2){
		  SelectTag.addOption(sel,ZONE_LIST[countryId][i],ZONE_LIST[countryId][i+1]);
	  }
  }else{
	  SelectTag.addOption(sel,"NA","-1");
  }

}


function showDataLine(inpTag,fdiscr){
  
 if (inpTag.get('type')!=Html.HIDDEN){
	document.write('<tr>');
    document.write('<td class="persdetl">'+fdiscr+'</td>');
    document.write('<td class="persdetr">'+inpTag.show()+'</td>');
    document.write('</tr>');
  }else{
	  document.write(inpTag.show());
  }
} 

function fastfill(){
  var f = document.fcheckout;
  f.elements['e_mailpass'].value=document.signinform.elements['e_mailpass'].value;
  f.elements['passpass'].value=document.signinform.elements['passpass'].value;
  Novigation.send_method=true;
  Novigation.moveTo(Novigation.PAGE_CART);
 }

function verifyPersonalInfo(f){

 var req_fields=new Array(0,1,2,3,4,5,6,7);
 for (var i=0;i<req_fields.length;i++) {
	var elem=f.elements[fields[req_fields[i]].get('name')];
	if (trim(elem.value)=="") {
				alert(strEFileds+fdescr[ fields[req_fields[i]].get('name') ]);
				elem.focus();
				return false;
	}
  }
  // check shipping fields
  if (f.elements['isshipping'] && f.elements['isshipping'].value==0){ 
	  req_fields=new Array(1,2,3,4,5,6,7);
	  for (var i=0;i<req_fields.length;i++) {
		var elem=f.elements[shipp_fields[req_fields[i]].get('name')];
		if (trim(elem.value)=="") {
			var tname=shipp_fields[req_fields[i]].get('name').substr(6);
			alert(strEFileds+fdescr[tname]);
			elem.focus();
			return false;
		}
	  }
  }
  return true;

}

function createPayHash(f){
  var paystr=new StringHash(); 
  var sel = f.elements['paymenttype'];
  var ptype=Payment.toType(SelectTag.seletedValue(sel));
  var ki=0;
  var value="";

  var hform=document.hform;
  for (var i=0;i<hform.elements.length;i++){
	    var field=hform.elements[i];
		var bolladd=true;
		if (field.name.indexOf("or_"+ptype+"_")==0){
			if (field.name.indexOf("or_"+ptype+"_paymenttypes")==0){
				 if (Html.checkElement(field)==Html.SELECT)	
						value=field.options[field.selectedIndex].text;
					else
						bolladd=false;
			}else
			     value=field.value;
		
			if (bolladd){
				paystr.add(ki,value);
				ki++;
			}

	   }
   }
  return paystr.toString();
}


function checkout(checkoutstage){

  var f = document.fcheckout;
  
  if (checkoutstage>2){
	if (!verifyPersonalInfo(f)) return;	

	if (checkoutstage>3){
		f.elements['mailprod'].value=ShoppingCart.getStrForMail();
		f.elements['lang'].value=MultiLang.lang;
		f.elements['total'].value=ShoppingCart.total;
		f.elements['payment'].value=createPayHash(f);
		f.elements['charge_ids'].value=ShoppingCart.getChargeIds();
	    for (var i=0;i<f.elements.length;i++)
		  f.elements[i].disabled="";

		Novigation.send_method=true;
	    Novigation.moveTo(Novigation.PAGE_THX);
		return;
	}

  }

  f.elements['checkoutstage'].value=checkoutstage;
  Novigation.moveTo(Novigation.PAGE_CART); 

  /*if (typeof(parent.Visual.ul)!=typeof(underfined))
	  f.elements['ref'].value=parent.Visual.ul.get('1')+""+parent.Visual.ul.get('0');*/

 }

function back(){
	Novigation.send_method=false;
	Url.go(Novigation.moveTo(Novigation.PAGE_START));
}

 
 function goSetHeight() {
   parent.setIframeHeight();
}

