var outputId = "" ;

function ajaxSynchronize(expression, msec)
{ 
	if (outputId != "") setTimeout('ajaxSynchronize('+expression+', '+msec+')', msec);
	else eval(expression);
}

function ajaxRequestByURL(dest, _outputId)
{
	try 
	{
   		 xmlhttp_ajax = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
	}catch(e){
	}
	xmlhttp_ajax.onreadystatechange = ajaxCallbackByURL;
	outputId = _outputId;
	xmlhttp_ajax.open("GET", dest);
	xmlhttp_ajax.send(null);
}

function ajaxCallbackByURL()
{
	// if the readyState code is 4 (Completed)
	// and http status is 200 (OK) we go ahead and get the responseText
	// other readyState codes:
	// 0=Uninitialised 1=Loading 2=Loaded 3=Interactive

	if ((xmlhttp_ajax.readyState == 4) && (xmlhttp_ajax.status == 200)) {
		// xmlhttp.responseText object contains the response.

		document.getElementById(outputId).innerHTML = xmlhttp_ajax.responseText;
		execJS(document.getElementById(outputId));
		outputId = "";
	}
}


// ########################################################################


function ajaxApplyRequestByURL(dest, _outputId)
{
	try 
	{
   		 xmlhttp_ajax_apply = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
	}catch(e){
	}
	xmlhttp_ajax_apply.onreadystatechange = ajaxApplyCallbackByURL;
	outputId = _outputId;
	xmlhttp_ajax_apply.open("GET", dest);
	xmlhttp_ajax_apply.send(null);
}

function ajaxApplyCallbackByURL()
{
	// if the readyState code is 4 (Completed)
	// and http status is 200 (OK) we go ahead and get the responseText
	// other readyState codes:
	// 0=Uninitialised 1=Loading 2=Loaded 3=Interactive

	if ((xmlhttp_ajax_apply.readyState == 4) && (xmlhttp_ajax_apply.status == 200)) {
		// xmlhttp.responseText object contains the response.
		document.getElementById(outputId).innerHTML = xmlhttp_ajax_apply.responseText;

		if (outputId == "certificate_redeem")
		{
			document.getElementById('total_sum_span').innerHTML = calculateTotalPrice( document.forms.ml_temp_data.subtotaltotal_sum.value, document.forms.save_first_step.tax_sum.value,	document.forms.save_first_step.shipping_price.value, document.getElementById('certificates_amount').value);
		   document.getElementById('total_sum').value = calculateTotalPrice( document.forms.ml_temp_data.subtotaltotal_sum.value, document.forms.save_first_step.tax_sum.value,	document.forms.save_first_step.shipping_price.value, document.getElementById('certificates_amount').value);
		}

//		alert (document.forms.ml_temp_data.subtotaltotal_sum.value);
//		alert (document.forms.save_first_step.tax_sum.value);
//		alert (document.forms.save_first_step.shipping_price.value);
//		alert (document.getElementById('certificates_amount').value);

		execJS(document.getElementById(outputId));
		outputId = "";
	}
}


function showPopup(nodeId)
{
//document.getElementById(nodeId).innerHTML = "";
document.getElementById(nodeId).style.display = 'block'; 
}

function hidePopup(nodeId)
{
document.getElementById(nodeId).style.display = 'none';
}




  var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
  var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
  var bMoz = (navigator.appName == 'Netscape');

// Function to allow launch javascript calls from innerHTML loaded content 
//(without this function it will not work - cause page are already loaded)
function execJS(node) {

  if (!node) return;

  var st = node.getElementsByTagName('SCRIPT');
  var strExec;
  for(var i=0;i<st.length; i++) {     
    if (bSaf) {
      strExec = st[i].innerHTML;
    }
    else if (bOpera) {
      strExec = st[i].text;
    }
    else if (bMoz) {
      strExec = st[i].textContent;
    }
    else {
      strExec = st[i].text;
    }
    try {
      eval(strExec.split("<!--").join("").split("-->").join(""));
    } catch(e) {
      alert(e);
    }
  }
}

function ajaxRequestShipToAdressByURL(dest)
{
	try 
	{
   		 xmlhttp_ajax = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
	}catch(e){
	}
	xmlhttp_ajax.onreadystatechange = ajaxCallbackShipToAddressByURL;
	xmlhttp_ajax.open("GET", dest);
	xmlhttp_ajax.send(null);
}

function ajaxRequestBillToAdressByURL(dest)
{
	try 
	{
   		 xmlhttp_ajax = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
	}catch(e){
	}
	xmlhttp_ajax.onreadystatechange = ajaxCallbackBillToAddressByURL;
	xmlhttp_ajax.open("GET", dest);
	xmlhttp_ajax.send(null);
}

function ajaxCallbackBillToAddressByURL()
{
	// if the readyState code is 4 (Completed)
	// and http status is 200 (OK) we go ahead and get the responseText
	// other readyState codes:
	// 0=Uninitialised 1=Loading 2=Loaded 3=Interactive
	if ((xmlhttp_ajax.readyState == 4) && (xmlhttp_ajax.status == 200))
	{
		// parse response and fill BILL TO info
		var resAddressInfo = xmlhttp_ajax.responseText;
		if ( resAddressInfo != "-1" )
		{
			var regexp = /[\w\s@.]*===/g;
			var addressValues = resAddressInfo.match(regexp);
			var theForm = document.forms.checkout_info_form;
			theForm.bill_firstname.value             = addressValues[0].substr(0, addressValues[0].indexOf("==="));
			theForm.bill_lastname.value              = addressValues[1].substr(0, addressValues[1].indexOf("==="));
			theForm.bill_companyname.value           = addressValues[2].substr(0, addressValues[2].indexOf("==="));
			theForm.bill_address1.value              = addressValues[3].substr(0, addressValues[3].indexOf("==="));
			theForm.bill_city.value                  = addressValues[4].substr(0, addressValues[4].indexOf("==="));
			theForm.bill_state.value                 = addressValues[5].substr(0, addressValues[5].indexOf("==="));
			theForm.bill_zip.value                   = addressValues[6].substr(0, addressValues[6].indexOf("==="));
			theForm.bill_phone.value                 = addressValues[7].substr(0, addressValues[7].indexOf("==="));
			theForm.bill_email.value                 = addressValues[8].substr(0, addressValues[8].indexOf("==="));
			document.getElementById("billRecordName").value = addressValues[9].substr(0, addressValues[9].indexOf("==="));
			theForm.billto_address_book_sel_id.value = theForm.billto_user_addresses.value;
			// visible radio and save checkout
			document.getElementById("add_to_billto_address_book").disabled = true;
			document.getElementById("add_to_billto_address_book_span").style.visibility = "hidden";
		}
	}
}

function ajaxCallbackShipToAddressByURL()
{
	// if the readyState code is 4 (Completed)
	// and http status is 200 (OK) we go ahead and get the responseText
	// other readyState codes:
	// 0=Uninitialised 1=Loading 2=Loaded 3=Interactive

	if ((xmlhttp_ajax.readyState == 4) && (xmlhttp_ajax.status == 200))
	{
		// parse response and fill SHIP TO info
		var resAddressInfo = xmlhttp_ajax.responseText;
		if ( resAddressInfo != "-1" )
		{
			var regexp = /[\w\s]*===/g;
			var addressValues = resAddressInfo.match(regexp);
			var theForm = document.forms.checkout_info_form;
			theForm.shipto_firstname.value    = addressValues[0].substr(0, addressValues[0].indexOf("==="));
			theForm.shipto_lastname.value     = addressValues[1].substr(0, addressValues[1].indexOf("==="));
			theForm.shipto_address1.value     = addressValues[2].substr(0, addressValues[2].indexOf("==="));
			theForm.shipto_address2.value     = addressValues[3].substr(0, addressValues[3].indexOf("==="));
			theForm.shipto_address3.value     = addressValues[4].substr(0, addressValues[4].indexOf("==="));
			theForm.shipto_city.value         = addressValues[5].substr(0, addressValues[5].indexOf("==="));
			theForm.shipto_state.value        = addressValues[6].substr(0, addressValues[6].indexOf("==="));
			theForm.shipto_zip.value          = addressValues[7].substr(0, addressValues[7].indexOf("==="));
			theForm.shipto_phone.value        = addressValues[8].substr(0, addressValues[8].indexOf("==="));
			document.getElementById("shipRecordName").value = addressValues[9].substr(0, addressValues[9].indexOf("==="));
			theForm.shipto_address_book_sel_id.value = theForm.shipto_user_addresses.value;
			// visible radio and save checkout
			document.getElementById("add_to_shipto_address_book").disabled = true;
			document.getElementById("add_to_shipto_address_book_span").style.visibility = "hidden";
		}
	}
}

function loadAddressBook(id, sType)
{
	if ( id != -1)
	{
		if ( 'bill' == sType )
		{
			ajaxRequestBillToAdressByURL('index.php?a=5.11&record_id=' + id + '&stype=' + sType);
		}
		else if ( 'ship' == sType )
		{
			ajaxRequestShipToAdressByURL('index.php?a=5.11&record_id=' + id + '&stype=' + sType);
		}
	}
	else
	{
		if ('ship' == sType)
		{
			clearShipToFields();
		}
		else if ('bill' == sType)
		{
			clearBillToFields();
		}
	}
}

function clearShipToFields()
{
	var theForm = document.forms.checkout_info_form;
	theForm.shipto_firstname.value     = "";
	theForm.shipto_lastname.value      = "";
	theForm.shipto_address1.value      = "";
	theForm.shipto_address2.value      = "";
	theForm.shipto_address3.value      = "";
	theForm.shipto_city.value          = "";
	theForm.shipto_state.selectedIndex = 0;
	theForm.shipto_zip.value           = "";
	theForm.shipto_phone.value         = "";
	theForm.shipto_address_book_sel_id.value  = "";

	// visible save checkout and hidden radio
	document.getElementById("shipRecordName").value = "";
	document.getElementById("add_to_shipto_address_book").disabled = false;
	document.getElementById("add_to_shipto_address_book_span").style.visibility = "visible";
}

function clearBillToFields()
{
	var theForm = document.forms.checkout_info_form;
	theForm.bill_firstname.value      = "";
	theForm.bill_lastname.value       = "";
	theForm.bill_companyname.value    = "";
	theForm.bill_address1.value       = "";
	theForm.bill_city.value           = "";
	theForm.bill_state.selectedIndex  = 0;
	theForm.bill_zip.value            = "";
	theForm.bill_phone.value          = "";
	theForm.bill_email.value          = "";
	theForm.billto_address_book_sel_id.value  = "";

	// visible save checkout and hidden radio
	document.getElementById("billRecordName").value = "";
	document.getElementById("add_to_billto_address_book").disabled = true;
	document.getElementById("add_to_billto_address_book_span").style.visibility = "hidden";
}

function delAddressBook(sType)
{
	if ('bill' == sType)
	{
		var userAddressesSelect = document.getElementById("billto_user_addresses");
	}
	else if ('ship' == sType)
	{
		var userAddressesSelect = document.getElementById("shipto_user_addresses");
	}
	if ( userAddressesSelect.selectedIndex > 0 )
	{
		if( confirm("Are you sure want to delete the selected record?"))
		{
			var deletedValue = userAddressesSelect.value;
			if ('bill' == sType)
			{
					userAddressesSelect.remove(document.getElementById("billto_user_addresses").selectedIndex);
					clearBillToFields();
					ajaxRequestBillToAdressByURL('index.php?a=5.11&do=del_data&del_id=' + deletedValue);
					if ( 1 == userAddressesSelect.options.length )
					{	
						userAddressesSelect.disabled = true;
						document.getElementById("user_billto_address_delete_img").style.visibility = "hidden";
					}
				}	
			else if ('ship' == sType)
			{
					userAddressesSelect.remove(document.getElementById("shipto_user_addresses").selectedIndex);
					clearShipToFields();
					ajaxRequestShipToAdressByURL('index.php?a=5.11&do=del_data&del_id=' + deletedValue);
					if ( 1 == userAddressesSelect.options.length )
					{	
						userAddressesSelect.disabled = true;
						document.getElementById("user_shipto_address_delete_img").style.visibility = "hidden";
					}
				}	
			}
		}
	else
	{
		alert ("Please, select Address Book Record.");		
	}
}
