
function deliveryToggle(togval) {

	var div1 = document.getElementById('delivery_details');

	if(togval==0) {
		div1.style.display = 'none';
	} else {
		div1.style.display = 'block';
	}

}


function openDropDownMenu(s_item) {

	document.getElementById('dd_' + s_item).className ="dropdownmenu_open";

}

function closeDropDownMenu(s_item) {

	document.getElementById('dd_' + s_item).className ="dropdownmenu";

}

function setValue(s_item, s_value){

	pid = document.getElementById('pid').value;
	currency = document.getElementById('currency').value;
	product_name = document.getElementById('product_name').value;

	document.getElementById('hidden_' + s_item).value = s_value;
	document.getElementById('dd_' + s_item).className ="dropdownmenu";

	new Ajax.Updater('title_' + s_item, '/data/credjewellery/scripts/ajax_shop.php', { 
		method: 'get',
		parameters: {option: 'set_dropdown', s_item: s_item, s_value: s_value, pid: pid} 
		
	});

	//set variables
	var fields = "metal,width,stonesizecarat,diamonds,fingersize";
	var s_fieldsincluded = "";

	x=0;
	fields = fields.split(",");
	var fields_value = new Array();
	while(fields[x]) {
		fields_value[x] = "";
		if(document.getElementById('dd_' + fields[x])) {
			if(document.getElementById('dd_' + fields[x]).className=="dropdownmenu") {
				s_fieldsincluded += fields[x] + ":" + document.getElementById('hidden_' + fields[x]).value + ";";
				if ( s_item == fields[x] ) {
					fields_value[x] = s_value;
				} else {
					fields_value[x] = document.getElementById('hidden_' + fields[x]).value;
				}
			}
		}
		x++;
	}
	x=0;

	// get total price
	 new Ajax.Updater('ajax_prices', '/data/credjewellery/scripts/ajax_shop.php', { 
		method: 'get',
		parameters: {option: 'get_prices', pid: pid, fieldsincluded: s_fieldsincluded, currency: currency} 
		
	});

	// set the url values

	$("url").value = window.location.href;
	if($("hidden_metal")) { $("url").value += "?metal=" + $("hidden_metal").value;}
	if($("hidden_stonesizecarat")) { $("url").value += "&stonesizecarat=" + $("hidden_stonesizecarat").value;}
	if($("hidden_width")) { $("url").value += "&width=" + $("hidden_width").value;}
	if($("hidden_diamonds")) { $("url").value += "&diamonds=" + $("hidden_diamonds").value;}
	if($("hidden_fingersize")) { $("url").value += "&fingersize=" + $("hidden_fingersize").value;}
	while ( $("url").value.search( " " ) != -1 ){
		$("url").value = $("url").value.replace(" ", "%20");
	}

	getImage();
}


function setCurrency(currency) {

	// If a currency is not set then obtain the active currency from the page form
	if ( !currency ){
		currency = document.getElementById('currency').value;
	}

	s_value = document.getElementById('total_price').value;
	pid = document.getElementById('pid').value;
	document.getElementById('dd_currency').className ="dropdownmenu";

	 new Ajax.Updater('title_currency', '/data/credjewellery/scripts/ajax_shop.php', { 
		method: 'get',
		parameters: {option: 'set_currency', currency: currency} 
		
	});


	//set variables
	var fields = "metal,width,stonesizecarat,diamonds,fingersize";
	var s_fieldsincluded = "";

	x=0;
	fields = fields.split(",");
	while(fields[x]) {
		if (document.getElementById('dd_' + fields[x])) {
			if(document.getElementById('dd_' + fields[x]).className=="dropdownmenu") {
				s_fieldsincluded += fields[x] + ":" + document.getElementById('hidden_' + fields[x]).value + ";";
			}
		}
		x++;
	}
	x=0;


	// get total price
	 new Ajax.Updater('ajax_prices', '/data/credjewellery/scripts/ajax_shop.php', { 
		method: 'get',
		parameters: {option: 'get_prices', pid: pid, fieldsincluded: s_fieldsincluded, currency: currency} 
		
	});

}

function getImage(){
	pid = document.getElementById('pid').value;
	currency = document.getElementById('currency').value;
	product_name = document.getElementById('product_name').value;

	//set variables
	var fields = "metal,width,stonesizecarat,diamonds,fingersize";

	x=0;
	fields = fields.split(",");
	var fields_value = new Array();
	while(fields[x]) {
		fields_value[x] = "";
		if(document.getElementById('dd_' + fields[x])) {
			if(document.getElementById('dd_' + fields[x]).className=="dropdownmenu") {
				fields_value[x] = document.getElementById('hidden_' + fields[x]).value;
			}
		}
		x++;
	}
	x=0;

	Effect.Appear('product_image', { from: 1.0, to: 0.0, duration: 0 });

	// get product image
	 new Ajax.Updater('product_image', '/data/credjewellery/scripts/ajax_shop.php', { 
		method: 'get',
		parameters: {option: 'get_product_image', product_name: product_name, metal: fields_value[0], width: fields_value[1], diamonds: fields_value[3], image_naming_convention: image_naming_convention, pid: pid},
		onComplete: function(response){
			Effect.Appear('product_image', { duration: 0.6, from: 0.0 });

			if ( $$('.image_small') ) {
				$$('.image_small').each( function(element){
					element.observe("click", function(event){
						var img_id = element.readAttribute("id");
						img_id = img_id.substr(img_id.length-1,1);

						$$('.image_medium').each( function(medium_img){
							if ( medium_img.readAttribute("id") != ("image_medium"+img_id) ){
								Effect.Fade(medium_img, { duration: 0 });
							}
						});

						Effect.Appear( $('image_medium'+img_id), { duration: 0.4 });

						if ( $$('.image_large') ) {
							$$('.image_large').each( function(large_img){ 
								if ( large_img.readAttribute("id") != ("image_large"+img_id) ){
									Effect.Fade(large_img, { duration: 0.1 });
								} else {
									Effect.Appear(large_img, { duration: 0.2 });
								}
								Effect.Fade(large_img.down(0), { duration: 0.2 });
							});
						}
					});
				});
			}

			if ( $$('.image_large') ) {
				$$('.image_large').each( function(element){ 

					element.observe("click", function(event) {
						if ( element.down(0).style.display == "none" ){
							Effect.Appear(element.down(0), { duration: 0.4 });
						} else {
							Effect.Fade(element.down(0), { duration: 0.2 });
						}
					});

				});
			}

		}
	});

}

function updateFields() {

		for (i=document.checkout.rb_sameabove.length-1; i > -1; i--) {
			if (document.checkout.rb_sameabove[i].checked) {
			rb_val = i;
			}
		}

		if(rb_val==0) {
			copyBillingToDelivery();
		}

}

function myfunction()
{
  submitFields();
  document.checkout.submit();
}


function submitFields() {

		for (i=document.checkout.rb_sameabove.length-1; i > -1; i--) {
			if (document.checkout.rb_sameabove[i].checked) {
			rb_val = i;
			}
		}

		if(rb_val==0) {
			copyBillingToDelivery();		
		}

		return true;
}

function copyBillingToDelivery() {
	document.checkout.eshop_dfirstname.value=document.checkout.eshop_firstname.value;
	document.checkout.eshop_dlastname.value=document.checkout.eshop_lastname.value;
	document.checkout.eshop_dcompany.value=document.checkout.eshop_company.value;		
	document.checkout.eshop_daddress1.value=document.checkout.eshop_address1.value;
	document.checkout.eshop_daddress2.value=document.checkout.eshop_address2.value;
	document.checkout.eshop_dcity.value=document.checkout.eshop_city.value;
	document.checkout.eshop_dcounty.value=document.checkout.eshop_county.value;
	document.checkout.eshop_dcountry.value=document.checkout.eshop_country.value;
	document.checkout.eshop_dpostcode.value=document.checkout.eshop_postcode.value;
}

function clearDeliveryDetails() {
	document.checkout.eshop_dfirstname.value = "";
	document.checkout.eshop_dlastname.value = "";
	document.checkout.eshop_dcompany.value = "";
	document.checkout.eshop_daddress1.value = "";
	document.checkout.eshop_daddress2.value = "";
	document.checkout.eshop_dcity.value = "";
	document.checkout.eshop_dcounty.value = "";
	document.checkout.eshop_dcountry.value = "";
	document.checkout.eshop_dpostcode.value = "";
}

function toggle(val) {

	var e = document.getElementById(val);
   if(!e) return true;
      if(e.style.display == "none")
      {
           e.style.display = "block"
     }
      else
      {
           e.style.display = "none"
      }
      return true; 

}

function validateCard(passedObject) {
	//	Get data

	if(passedObject=="admin") {
		eshop_cardtype = $F("table_row[eshop_cardtype]");
		eshop_cardnumber = $("table_row[eshop_cardnumber]").value;
		eshop_securitycode = $("table_row[eshop_securitycode]").value;
		eshop_issuenumber = $("table_row[eshop_issuenumber]").value;
		eshop_cardholdername = $("table_row[eshop_cardholdername]").value;
		eshop_validfrom = (Right("0" + $("table_row[eshop_validfrom]").value,2)) + ($("table_row[eshop_validfromyear]").value.substring(2,4));
		eshop_expirydate = (Right("0" + $("table_row[eshop_expirydate]").value,2)) + ($("table_row[eshop_expirydateyear]").value.substring(2,4));
		eshop_cardholdername = $("table_row[eshop_cardholdername]").value;
	} else {
		eshop_cardtype = $F("eshop_cardtype");
		eshop_cardnumber = $("eshop_cardnumber").value;
		eshop_securitycode = $("eshop_securitycode").value;
		eshop_issuenumber = $("eshop_issuenumber").value;
		eshop_cardholdername = $("eshop_cardholdername").value;
		eshop_validfrom = (Right("0" + $F("eshop_validfrom"),2)) + ($F("eshop_validfromyear").substring(2,4));
		eshop_expirydate = (Right("0" + $F("eshop_expirydate"),2)) + ($F("eshop_expirydateyear").substring(2,4));
		eshop_cardholdername = $("eshop_cardholdername").value;
	}

	// get total price
	 new Ajax.Updater('credit_valid', '/data/credjewellery/scripts/ajax_shop.php', { 
		method: 'get',
		parameters: {	option: 'card_validation',
						eshop_cardtype: eshop_cardtype,
						eshop_cardnumber: eshop_cardnumber,
						eshop_securitycode: eshop_securitycode,
						eshop_issuenumber: eshop_issuenumber,
						eshop_cardholdername: eshop_cardholdername,
						eshop_validfrom: eshop_validfrom,
						eshop_expirydate: eshop_expirydate,
						eshop_cardholdername: eshop_cardholdername
					} 
	});


}

function checkValidation() {
	alert($("credit_valid").innerHTML.length);
	if($("credit_valid").innerHTML.length=="1") {
		document.checkout.submit();
	}
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function IsEmpty(aTextField) {
   if ((aTextField.value.length==0) ||
   (aTextField.value==null)) {
      return true;
   }
   else { return false; }
}
	


document.observe("dom:loaded", function() {
	
	if ( $('securityCode') ){

		//Relative Window / Content from Ajax  
		var ajax = new Control.Window($('securityCode'),{  
		    className: 'simple_window',
			closeOnClick: true,
		    position: 'relative',
			offsetLeft: 95,
			offsetTop: -250
		});
	}

	if ( $('b_diff_delivery_details') ){
		var typeValue = Form.getInputs('checkout','radio','b_diff_delivery_details').find(function(radio) { return radio.checked; }).value;
		if ( !typeValue ){
			$('delivery_details').hide();
		}
	}

});


