/*
  $Id: general.js 1739 2007-12-20 00:52:16Z hpdl $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/


/**
 * Register Event Listeners
 */
$(document).ready(function()
{
    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
        //Get the A tag
        var id = $(this).attr('href');

        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});

        //transition effect
        $('#mask').fadeIn(1);
        $('#mask').fadeTo("slow",0.8);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);

        //transition effect
        $(id).fadeIn(1);

    });
	//select all the a tag with name equal to modal
    $('a[name=modal2]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
        //Get the A tag
        var id = $(this).attr('href');

        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#mask2').css({'width':maskWidth,'height':maskHeight});

        //transition effect
        $('#mask2').fadeIn(1);
        $('#mask2').fadeTo("slow",0.8);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);

        //transition effect
        $(id).fadeIn(1);

    });

    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask, .window').hide();
		$('#mask2, .window').hide();
    });

    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });
	 //if mask is clicked
    $('#mask2').click(function () {
        $(this).hide();
        $('.window').hide();
    });

//end function document ready
});

/**
 *
 */
function SetFocus(TargetFormName) {
  var target = 0;
  if (TargetFormName != "") {
    for (i=0; i<document.forms.length; i++) {
      if (document.forms[i].name == TargetFormName) {
        target = i;
        break;
      }
    }
  }

  var TargetForm = document.forms[target];
    
  for (i=0; i<TargetForm.length; i++) {
    if ( (TargetForm.elements[i].type != "image") && (TargetForm.elements[i].type != "hidden") && (TargetForm.elements[i].type != "reset") && (TargetForm.elements[i].type != "submit") ) {
      TargetForm.elements[i].focus();

      if ( (TargetForm.elements[i].type == "text") || (TargetForm.elements[i].type == "password") ) {
        TargetForm.elements[i].select();
      }

      break;
    }
  }
}

function RemoveFormatString(TargetElement, FormatString) {
  if (TargetElement.value == FormatString) {
    TargetElement.value = "";
  }

  TargetElement.select();
}

function CheckDateRange(from, to) {
  if (Date.parse(from.value) <= Date.parse(to.value)) {
    return true;
  } else {
    return false;
  }
}

function IsValidDate(DateToCheck, FormatString) {
  var strDateToCheck;
  var strDateToCheckArray;
  var strFormatArray;
  var strFormatString;
  var strDay;
  var strMonth;
  var strYear;
  var intday;
  var intMonth;
  var intYear;
  var intDateSeparatorIdx = -1;
  var intFormatSeparatorIdx = -1;
  var strSeparatorArray = new Array("-"," ","/",".");
  var strMonthArray = new Array("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
  var intDaysArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  strDateToCheck = DateToCheck.toLowerCase();
  strFormatString = FormatString.toLowerCase();
  
  if (strDateToCheck.length != strFormatString.length) {
    return false;
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strFormatString.indexOf(strSeparatorArray[i]) != -1) {
      intFormatSeparatorIdx = i;
      break;
    }
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strDateToCheck.indexOf(strSeparatorArray[i]) != -1) {
      intDateSeparatorIdx = i;
      break;
    }
  }

  if (intDateSeparatorIdx != intFormatSeparatorIdx) {
    return false;
  }

  if (intDateSeparatorIdx != -1) {
    strFormatArray = strFormatString.split(strSeparatorArray[intFormatSeparatorIdx]);
    if (strFormatArray.length != 3) {
      return false;
    }

    strDateToCheckArray = strDateToCheck.split(strSeparatorArray[intDateSeparatorIdx]);
    if (strDateToCheckArray.length != 3) {
      return false;
    }

    for (i=0; i<strFormatArray.length; i++) {
      if (strFormatArray[i] == 'mm' || strFormatArray[i] == 'mmm') {
        strMonth = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'dd') {
        strDay = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'yyyy') {
        strYear = strDateToCheckArray[i];
      }
    }
  } else {
    if (FormatString.length > 7) {
      if (strFormatString.indexOf('mmm') == -1) {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mm'), 2);
      } else {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mmm'), 3);
      }

      strDay = strDateToCheck.substring(strFormatString.indexOf('dd'), 2);
      strYear = strDateToCheck.substring(strFormatString.indexOf('yyyy'), 2);
    } else {
      return false;
    }
  }

  if (strYear.length != 4) {
    return false;
  }

  intday = parseInt(strDay, 10);
  if (isNaN(intday)) {
    return false;
  }
  if (intday < 1) {
    return false;
  }

  intMonth = parseInt(strMonth, 10);
  if (isNaN(intMonth)) {
    for (i=0; i<strMonthArray.length; i++) {
      if (strMonth == strMonthArray[i]) {
        intMonth = i+1;
        break;
      }
    }
    if (isNaN(intMonth)) {
      return false;
    }
  }
  if (intMonth > 12 || intMonth < 1) {
    return false;
  }

  intYear = parseInt(strYear, 10);
  if (isNaN(intYear)) {
    return false;
  }
  if (IsLeapYear(intYear) == true) {
    intDaysArray[1] = 29;
  }

  if (intday > intDaysArray[intMonth - 1]) {
    return false;
  }
  
  return true;
}

function IsLeapYear(intYear) {
  if (intYear % 100 == 0) {
    if (intYear % 400 == 0) {
      return true;
    }
  } else {
    if ((intYear % 4) == 0) {
      return true;
    }
  }

  return false;
}


/**
*	Open popup to cancel order
*/
function open_popup_show_requested(){
	var iPopup = document.getElementById('cancel_requested');
	if(iPopup.style.display == 'none'){
		document.getElementById('cancel_order').style.display='none';
		iPopup.style.display = 'block';
		iPopup.style.left ="30%";
		iPopup.style.top = "30%";
	}
	else{
		iPopup.style.display = 'none';
	}
}

/**
*	Open popup 
*/
function open_popup(elementID){
	var iPopup = document.getElementById(elementID);
	if(iPopup.style.display == 'none'){
		iPopup.style.display = 'block';
		iPopup.style.left ="35%";
		iPopup.style.top = "25%";
	}
	else{
		iPopup.style.display = 'none';
	}
}

/**
*	Open popup 
*/
function open_popup_edit_address(elementID){
	var iPopup = document.getElementById(elementID);
	if(iPopup.style.display == 'none'){
		iPopup.style.display = 'block';
		iPopup.style.left ="25%";
		iPopup.style.top = "25%";
	}
	else{
		iPopup.style.display = 'none';
	}
}

function getXMLHTTP() { //fuction to return the xml http object
		var xmlhttp=false;	
		try{
			xmlhttp=new XMLHttpRequest();
		}
		catch(e)	{		
			try{			
				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){
				try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e1){
					xmlhttp=false;
				}
			}
		}
		 	
		return xmlhttp;
    }

/**
 *
 */
function updateAddressForm(form) {		
    var entries=getEntries(form);
    var strURL="address_book_process2.php?"+entries;
    var type=form.elements['type'].value;
    var req = getXMLHTTP();

    if (req) {

        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                // only if "OK"
                if (req.status == 200) {
                    if(req.responseText!='')
                        updateAddress(req.responseText,type,entries);
                    else{
                        document.getElementById('refresh').submit();
                    }
                } else {
                    alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                }
            }
        }
        req.open("POST", strURL, true);
        req.send(null);

    }else {
        return false;
    }

}
			function updateAddress(addresID,type,entries) {
		
		var strURL="address_book_details2.php?addressid="+addresID+"&type="+type+"&"+entries;

		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {						
						document.getElementById('editaddress').innerHTML=req.responseText;						
						show();						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("POST", strURL, true);
			req.send(null);
		}
		else
		return false		
	}
	
	
	function getEntries(form){
	
	var poststr = "firstname="+form.elements['firstname'].value+
                 "&lastname="+form.elements['lastname'].value+
				 "&middle="+form.elements['middle'].value+
				 "&street_address="+form.elements['street_address'].value+
				 "&suburb="+form.elements['suburb'].value+
				 "&city="+form.elements['city'].value+
				 "&postcode="+form.elements['postcode'].value+ 
				 "&country="+form.elements['country'].value+ 
				 "&telephone="+form.elements['telephone'].value+ 
				 "&fax="+form.elements['fax'].value+
				 "&action="+form.elements['action'].value+ 
				 ( (form.elements['state']) ? ("&state="+form.elements['state'].value) : '' )+
				 "&edit="+form.elements['edit'].value
				 ;
    	if(form.elements['stated'])			 
		poststr+= "&stated="+form.elements['stated'].value;
		poststr=poststr.replace(/#/g,'%23');

	return poststr;
	}
	
	function getEntries2(form){
	
	var poststr2 ="&cc_type="+form.elements['cc_type'].value+
				 "&authorizenet_aim_cc_number="+form.elements['authorizenet_aim_cc_number'].value+
				 "&authorizenet_aim_cc_expires_year="+form.elements['authorizenet_aim_cc_expires_year'].value+
				 "&authorizenet_aim_cc_expires_month="+form.elements['authorizenet_aim_cc_expires_month'].value+
				 "&authorizenet_aim_cc_cvv="+form.elements['authorizenet_aim_cc_cvv'].value;
				 
    	/*if(form.elements['stated'])			 
		poststr+= "&stated="+form.elements['stated'].value;
		poststr=poststr.replace(/#/g,'%23');*/

	return poststr2;
	}
	
/***********CREDITCARD FUNCTIONS FUNCTIONS***********/

	function updateCreditCardForm(form) {		
		var entries=getEntries(form);
		var entries2=getEntries2(form);
		var strURL="checkout_payment_add_cc_process.php?"+entries+entries2;
		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {	
						if(req.responseText!='')
						updateCreditCard(entries,entries2);
						else		
						document.refreshform.submit();
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("POST", strURL, true);
			req.send(null);
		}
		else
		return false			
	}
	
		
	function updateCreditCard(entries,entries2) {		
		
		if(entries != '' && entries2 != '' )
		var strURL="checkout_payment_add_cc.php?"+"&"+entries+entries2;
		else
		var strURL="checkout_payment_add_cc.php";
		
		var req = getXMLHTTP();
		
		if (req) {
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {						
						document.getElementById('editCC').innerHTML=req.responseText;						
						show();						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("POST", strURL, true);
			req.send(null);
		}
		else
		return false	
		
	}

	/***********END CREDITCARD FUNCTIONS FUNCTIONS***********/
	
	function getState(countryId) {		
		
		var strURL="find_state.php?country="+countryId;
		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {						
						document.getElementById('statediv').innerHTML=req.responseText;				
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("POST", strURL, true);
			req.send(null);
		}
		else
		return false
				
	}

	function updateAddress(addresID,type,entries) {
		
		var strURL="address_book_details2.php?addressid="+addresID+"&type="+type+"&"+entries;

		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {						
						document.getElementById('editaddress').innerHTML=req.responseText;						
						show();						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("POST", strURL, true);
			req.send(null);
		}
		else
		return false		
	}
	
/***********CONTACT US FUNCTIONS***********/
	
	function updateContactus(entries) {
		var strURL="contactus_details.php?"+entries;
		var req = getXMLHTTP();
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {		
						document.getElementById('contactus').innerHTML=req.responseText;						
						show();						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("POST", strURL, true);
			req.send(null);
		}
		else
		return false		
	}
	
	function updateContactusForm(form) {		
		var entries=getEntriesContactus(form);
		var strURL="contactus_process.php?"+entries;
		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {	
						if(req.responseText!='')
						updateContactus(entries);
						else		
						Contactus_done();
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("POST", strURL, true);
			req.send(null);
		}
		else
		return false			
	}
	
	function getEntriesContactus(form){
		var poststr = "firstname="+form.elements['firstname'].value+
					 "&lastname="+form.elements['lastname'].value+
					 "&middle="+form.elements['middle'].value+
					 "&email_address="+form.elements['email_address'].value+
					 "&inquery="+form.elements['inquery'].value+
					 "&message="+form.elements['message'].value;
					 
		return poststr;
	}
	
	
/**********END CONTACT US FUNCTIONS************/

	function send_requestjs(id) {
		var reason = document.forms['cancelation_order'].elements['reason'].value;
		var comments = document.forms['cancelation_order'].elements['comments'].value;
		updateCancelOrderForm(id,reason,comments,'2');		
	}

	
	function updateCancelOrderForm(id,reason,comments,type) {
		var strURL="cancel_order.php?id="+id+"&reason="+reason+"&comments="+comments+"&type="+type;
		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {	
						if(req.responseText!='go')
						updateCancelOrder(id,reason,comments,type);
						else		
						cancelOrderSubmited(id);
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("POST", strURL, true);
			req.send(null);
		}
		else
		return false			
	}
	
	function updateCancelOrder(id,reason,comments,type) {
		
		var strURL="cancel_order.php?id="+id+"&reason="+reason+"&comments="+comments+"&type="+type;
		
		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {						
						document.getElementById('cancel_order').innerHTML=req.responseText;						
						show();						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("POST", strURL, true);
			req.send(null);
		}
		else
		return false		
	}
		
	function selectAddress(address_id, customerID) {
		var strURL="checkout_selectAnotherAddress.php?customer_id="+customerID+"&sendto=" + address_id;
		var req = getXMLHTTP();
		if (req) {
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {
						document.getElementById('selectAnother').innerHTML=req.responseText;
						show2();						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("POST", strURL, true);
			req.send(null);
		}
		else
		return false		
	}	

function show()
{
$("#modal").trigger("click");
}

function hidepopup()
{
$('#mask, .window').hide();
$('#mask, .smallwindow').hide();
}

function show2()
{
$("#modal2").trigger("click");
}

function hidepopup2()
{
$('#mask2, .window').hide();
$('#mask2, .smallwindow').hide();
}

function cancelOrderSubmited(order_id) {		
		
		var strURL="cancel_order_submited.php?orderid="+order_id;
		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {						
						document.getElementById('cancel_order').innerHTML=req.responseText;					
						show();						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("POST", strURL, true);
			req.send(null);
		}
		else
		return false		
	}	
	
	function Contactus_done() {		
		
		var strURL="contactus_done.php";
		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {						
						document.getElementById('contactus').innerHTML=req.responseText;					
						show();						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("POST", strURL, true);
			req.send(null);
		}
		else
		return false		
	}
	
	
function printOrder (url){
//url="print_order.php?order="+id;
window.open (url,"mywindow","location=0,status=1,scrollbars=1");	
}

function printOrder_Submitted(url){
//url="print_order2.php"
window.open (url,"mywindow","location=0,status=1,scrollbars=1");	
}

// Do nothing when an AJAX call is been excecuted	
function do_nothing(result){
	//alert (result);
}
function openCVVpopup(window_name){
	var strURL=window_name;
	var req = getXMLHTTP();
	if (req) {
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				// only if "OK"
				if (req.status == 200) {						
					document.getElementById('popup_cvv').innerHTML=req.responseText;						
					show();						
				} else {
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		}			
		req.open("POST", strURL, true);
		req.send(null);
	}
}