/*
  $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
*/

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;
    }
  }
}

/* from product info page */
function popupWindow(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=500,height=300,top=150,left=150')
}

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 check_form_optional(form_name) {
  var form = form_name;

  var firstname = form.elements['firstname'].value;
  var lastname = form.elements['lastname'].value;
  var street_address = form.elements['street_address'].value;

  if (firstname == '' && lastname == '' && street_address == '') {
    return true;
  } else {
    return check_form(form_name);
  }
}


/* function to show popup window displaying all images */
function showPopupWindow(iProduct,iNum)
{
    win = window.open('','WinImage','width=700,height=500,status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=no');
    document.cart_quantity.target = 'WinImage';
    document.cart_quantity.action = "product_info.php?action=viewimages&product="+iProduct+"&image="+iNum;
    document.cart_quantity.method = "post";
    document.cart_quantity.submit();
}
/* Function to add to cart*/
function addToCart(strAction)
{
        document.cart_quantity.action = strAction;
	document.cart_quantity.target = '_self';
	document.cart_quantity.method = "post";
	document.cart_quantity.submit();
        
}
function funcDeleteProduct(iProduct,osCsid)
{
        strAction = 'shopping_cart.php?action=remove_product&product=' + iProduct;
        if(osCsid != "")
        strAction = strAction + '&osCsid='+osCsid;
        document.cart_quantity.action = strAction ;
        document.cart_quantity.submit();
}

function func_promotional_submit(osCsid)
{
      strAction = 'shopping_cart.php';
      if(osCsid != "")
      strAction = strAction + '?osCsid='+osCsid;
        
      document.cart_quantity.action= strAction; 
      document.cart_quantity.method="post";
      document.cart_quantity.submit();
}

function showVatRelief(url)
{
   position = url.indexOf('?', url);

   addChar = '?';
   if (position >= 0 )
   {
     addChar = '&';
   }
   
   if ($("#chkVatRelief").attr("checked"))
   {
        $("#excVat").show();
        $("#payTotalNoVAT").show();
        $("#subTotal").hide();
        $("#payTotalVAT").hide();
        postedValue = 'Y';
        
   }
   else
   {
         $("#excVat").hide();
         $("#payTotalNoVAT").hide();
         $("#subTotal").show();
         $("#payTotalVAT").show();
         postedValue = 'N';
        
   }
   
    $.ajax(
	    {
            url : url+addChar+"action=addVatRelief&checked="+postedValue,
            type:"post",
            dataType:"text",
            beforeSend: ajaxLoader,
            success:function(reply)
            {
                
            },
            error:function()
            {
                 alert("Could not complete request");
            },
            complete:   function()
            {
                $("#dvAjaxLoader").hide();
            }

	   });
}




function ajaxLoader()
{
    if($("#dvAjaxLoader").is(":visible"))
    {
        $("#dvAjaxLoader").hide();
    }
    else
    {
        var windowHeight = $(window).height(); //getWindowHeight();
        var windowWidth  = $(window).width(); //getWindowWidth();
        var loader       = $("#dvAjaxLoader");

        var scrollTop = document.documentElement.scrollTop ? document.documentElement.scrollTop : (document.body.scrollTop ? document.body.scrollTop : 0); //(document.documentElement.scrollTop);

        if (windowHeight > 0)
        {
            var loaderTop   = scrollTop + (windowHeight - loader.outerHeight()) / 2;
            var loaderLeft  = (windowWidth - loader.outerWidth()) / 2;

            loader.css({
                            position : "absolute",
                           // left : loaderLeft,
                            top : loaderTop
                        }).show();
        }
    }
}