//
// +----------------------------------------------------------------------+
// |zen-cart Open Source E-commerce                                       |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003 The zen-cart developers                           |
// |                                                                      |   
// | http://www.zen-cart.com/index.php                                    |   
// |                                                                      |   
// | Portions Copyright (c) 2003 osCommerce                               |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the GPL license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.zen-cart.com/license/2_0.txt.                             |
// | If you did not receive a copy of the zen-cart license and are unable |
// | to obtain it through the world-wide-web, please send a note to       |
// | license@zen-cart.com so we can mail you a copy immediately.          |
// +----------------------------------------------------------------------+
// $Id: general.js 1105 2005-04-04 22:05:35Z birdbrain $
//

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;
}
function addToCart(form){
    var ATCURL="http://www.carouseltoyshop.co.uk/index.php?main_page=product_info&amp;action=add_product";
	ATCURL+='&amp;cart_quantity='+form.elements['cart_quantity'].value;
	ATCURL+='&amp;products_id='+form.elements['products_id'].value;

        for (i = 0; i < 20; i++){
		radioa=document.getElementById('attr_radio_'+i);
		if (radioa) {
			for (j=0;j<1000;j++) {

				roption=document.getElementById('attrib-'+radioa.value+'-'+j);
				if (roption)
					if(roption.checked) ATCURL+='&amp;'+roption.name+'='+roption.value;
			}
		}

		select=document.getElementById('attr_select_'+i);
		if (select) {
			sel=document.getElementById('attrib-'+select.value);
                        ATCURL+='&amp;'+sel.name+'='+sel.options[sel.selectedIndex].value;
		}

	}
	loadShoppingCart(ATCURL);
	return false;
     }
    function calcSubTotal(number){
    var total=0.0;

    itemsTotal=0;
	
    for (i=1;i<=100;i++){
	if (document.getElementById("qty_"+i))    itemsTotal++;
    }

    items=0;
	for (i=1;i<=itemsTotal;i++){
	        if (isFinite(document.getElementById("qty_"+i).value)) {
                        items=items+document.getElementById("qty_"+i).value*1;
			total=total+document.getElementById("qty_"+i).value*document.getElementById("prc_"+i).value;
			document.getElementById("subtot_"+i).innerHTML='&#163;'+Math.round(document.getElementById("qty_"+i).value*document.getElementById("prc_"+i).value*100)/100;
		} else {
		alert ('Quantity must be number! Please correct it.');
		}
	}

    document.getElementById('subtotal').innerHTML='&#163;'+Math.round(total*100)/100;	
    document.getElementById("cartform").submit();

    var cart_text=document.getElementById('catrtext');

    if (cart_text){
        if (items==1) cart_text.innerHTML="one item on cart";
        else cart_text.innerHTML=items+' x items on cart';

	document.getElementById('carttotal').innerHTML=document.getElementById('subtotal').innerHTML;
    }
    }
function check_sform() {
  var error_message = "Errors have occurred during the processing of your form.\n\nPlease make the following corrections:\n\n";
  var error_found = false;
  var error_field;
  var keyword = document.advanced_search.keyword.value;
  var dfrom = document.advanced_search.dfrom.value;
  var dto = document.advanced_search.dto.value;
  var pfrom = document.advanced_search.pfrom.value;
  var pto = document.advanced_search.pto.value;
  var pfrom_float;
  var pto_float;

  if (((keyword = '') || (keyword.length < 1)) && ((dfrom = '') || (dfrom == 'dd/mm/yyyy') || (dfrom.length < 1)) && ((dto = '') || (dto == 'dd/mm/yyyy') || (keyword.length < 1)) && ((pfrom = '') || (pfrom.length < 1)) && ((pto = '') || (pto.length < 1))) {
    error_message = error_message + "* ERROR_AT_LEAST_ONE_INPUT\n";
    error_field = document.advanced_search.keyword;
    error_found = true;
  }

  if ((dfrom.length > 0) && (dfrom != 'dd/mm/yyyy')) {
    if (!IsValidDate(dfrom, 'dd/mm/yyyy')) {
      error_message = error_message + "* ERROR_INVALID_FROM_DATE\n";
      error_field = document.advanced_search.dfrom;
      error_found = true;
    }
  }

  if ((dto.length > 0) && (dto != 'dd/mm/yyyy')) {
    if (!IsValidDate(dto, 'dd/mm/yyyy')) {
      error_message = error_message + "* ERROR_INVALID_TO_DATE\n";
      error_field = document.advanced_search.dto;
      error_found = true;
    }
  }

  if ((dfrom.length > 0) && (dfrom != 'dd/mm/yyyy') && (IsValidDate(dfrom, 'dd/mm/yyyy')) && (dto.length > 0) && (dto != 'dd/mm/yyyy') && (IsValidDate(dto, 'dd/mm/yyyy'))) {
    if (!CheckDateRange(document.advanced_search.dfrom, document.advanced_search.dto)) {
      error_message = error_message + "* ERROR_TO_DATE_LESS_THAN_FROM_DATE\n";
      error_field = document.advanced_search.dto;
      error_found = true;
    }
  }

  if (pfrom.length > 0) {
    pfrom_float = parseFloat(pfrom);
    if (isNaN(pfrom_float)) {
      error_message = error_message + "* ERROR_PRICE_FROM_MUST_BE_NUM\n";
      error_field = document.advanced_search.pfrom;
      error_found = true;
    }
  } else {
    pfrom_float = 0;
  }

  if (pto.length > 0) {
    pto_float = parseFloat(pto);
    if (isNaN(pto_float)) {
      error_message = error_message + "* ERROR_PRICE_TO_MUST_BE_NUM\n";
      error_field = document.advanced_search.pto;
      error_found = true;
    }
  } else {
    pto_float = 0;
  }

  if ( (pfrom.length > 0) && (pto.length > 0) ) {
    if ( (!isNaN(pfrom_float)) && (!isNaN(pto_float)) && (pto_float < pfrom_float) ) {
      error_message = error_message + "* ERROR_PRICE_TO_LESS_THAN_PRICE_FROM\n";
      error_field = document.advanced_search.pto;
      error_found = true;
    }
  }

  if (error_found == true) {
    alert(error_message);
    error_field.focus();
    return false;
  } else {
    RemoveFormatString(document.advanced_search.dfrom, "dd/mm/yyyy");
    RemoveFormatString(document.advanced_search.dto, "dd/mm/yyyy");
    return true;
  }
}
     function searchAdvanced(form){
        var SURL="http://www.carouseltoyshop.co.uk/advanced_search_result.html";

	if (check_sform(form)) {
	        SURL+='?keyword='+escape(form.elements['keyword'].value);

		if (form.elements['search_in_description'].checked)
		        SURL+='&amp;search_in_description=1';

                var sel=form.elements['categories_id'];
                SURL+='&amp;categories_id='+sel.options[sel.selectedIndex].value;

		if (form.elements['inc_subcat'].checked)
		        SURL+='&amp;inc_subcat=1';

                var sel=form.elements['manufacturers_id'];
                SURL+='&amp;manufacturers_id='+sel.options[sel.selectedIndex].value;

	        SURL+='&amp;pfrom='+escape(form.elements['pfrom'].value);
	        SURL+='&amp;pto='+escape(form.elements['pto'].value);

	        SURL+='&amp;dfrom='+escape(form.elements['dfrom'].value);
	        SURL+='&amp;dto='+escape(form.elements['dto'].value);
	        loadCenter(SURL);
	}

	return false;
     }

    function delFromCart(number){
       if (confirm('Are you sure you want to delete this item?')) {
	       document.getElementById("row_"+number).style.display = 'none';
	       document.getElementById("qty_"+number).value=0;
	       calcSubTotal();
       }
    }
      function loadShoppingCart(URL){
        if (URL.indexOf('?')>0)
	        URL+='&amp;nocach='+Math.round(Math.random() * 100000000000);
	else 
	        URL+='?nocach='+Math.round(Math.random() * 100000000000);

        httpCenter.open('get', 'http://www.carouseltoyshop.co.uk/ajax.php?load=center&URL='+escape(URL));
	httpCenter.onreadystatechange = handleInfoShoppingCart;
        httpCenter.send(null);

      }

      function loadRight(URL){

	URL=URL.replace('/?','/index.php?');

        if (URL.indexOf('?')>0)
	        URL+='&amp;nocach='+Math.round(Math.random() * 100000000000);
	else 
	        URL+='?nocach='+Math.round(Math.random() * 100000000000);

        httpRight.open('get', 'http://www.carouseltoyshop.co.uk/ajax.php?load=right&URL='+escape(URL));
        httpRight.onreadystatechange = handleInfoRight;
        httpRight.send(null);

        return false;

      }
function loadPageHistory(URL,nohistory){
	URL=URL.replace('/?','/index.php?');
	if (nohistory) {
	        dhtmlHistory.add(makeAnchor(URL),URL);
		historyIndex++;
	}
        if (URL.indexOf('?')>0)
	        URL+='&amp;nocach='+Math.round(Math.random() * 100000000000);
	else 
	        URL+='?nocach='+Math.round(Math.random() * 100000000000);
        httpCenter.open('get', 'http://www.carouseltoyshop.co.uk/ajax.php?load=center&URL='+escape(URL));
	httpCenter.onreadystatechange = handleInfoCenter;
        httpCenter.send(null);   
        httpRight.open('get', 'http://www.carouseltoyshop.co.uk/ajax.php?load=right&URL='+escape(URL));
        httpRight.onreadystatechange = handleInfoRight;
        httpRight.send(null);

        return false;

      }

      function handleInfoCenter(){
        if(httpCenter.readyState == 1){
          document.getElementById('content').innerHTML = 'Loading...';
        }
        if(httpCenter.readyState == 4){
          var response = httpCenter.responseText;

          if (response.indexOf('<!--reloadme-->')==0){
	          document.getElementById('content').innerHTML = 'Redirecting...';
                  loadCenter(response.substring(15));
	  } else document.getElementById('content').innerHTML = response;

        }
      }

      function handleInfoShoppingCart(){
        if(httpCenter.readyState == 1){
          document.getElementById('content').innerHTML = 'Loading...';
        }
        if(httpCenter.readyState == 4){
          var response = httpCenter.responseText;

          if (response.indexOf('<!--reloadme-->')==0){
	          document.getElementById('content').innerHTML = 'Redirecting...';
                  loadShoppingCart(response.substring(15));
	  } else {
		document.getElementById('content').innerHTML = response;
  	        loadRight('http://www.carouseltoyshop.co.uk/shopping_cart.html');
	  }

        }
      }


      function handleInfoRight(){
        if(httpRight.readyState == 1){
          if (document.getElementById('rightcolumn'))
	          document.getElementById('rightcolumn').innerHTML = 'Loading...';
        }
        if(httpRight.readyState == 4){
          var response = httpRight.responseText;

          if (response.indexOf('<!--reloadme-->')==0){
	          document.getElementById('rightcolumn').innerHTML = 'Redirecting...';
              loadRight(response.substring(15));
	  } else document.getElementById('rightcolumn').innerHTML = response;

        }
      }

     function searchBox(){
        var SURL="http://www.carouseltoyshop.co.uk/advanced_search_result.html?keyword=";
        SURL+=escape(document.getElementById("boxkeyword").value);
        loadCenter(SURL);
	return false;
     }
	       function loadPage(URL){
	       return loadPageHistory(URL,true);
      }

      function loadCenter(URL){
      dhtmlHistory.add(makeAnchor(URL),URL);
      historyIndex++;
      URL=URL.replace('/?','/index.php?');

        if (URL.indexOf('?')>0)
	        URL+='&amp;nocach='+Math.round(Math.random() * 100000000000);
	else 
	        URL+='?nocach='+Math.round(Math.random() * 100000000000);
        httpCenter.open('get', 'http://www.carouseltoyshop.co.uk/ajax.php?load=center&amp;URL='+escape(URL));
	httpCenter.onreadystatechange = handleInfoCenter;
        httpCenter.send(null);
      }