/*
  $Id: general.js,v 1.3 2003/02/10 22:30:55 hpdl Exp $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/
function createRequestObject() 
{
		var ro;
		var browser = navigator.appName;
		if(browser == "Microsoft Internet Explorer")
		{
			ro = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else
		{
			ro = new XMLHttpRequest();
		}
		return ro;
}
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 openPopup(url)
{
	var popwindow=window.open(url,'gocomp','width=590,height=210,top=300,left=50,resizable=no,location=0,navigation=0,titlebar=0,addressbar=0,toolbar=0,status=0,fullscreen=no');
	popwindow.focus();
	return false;
}

function bookmarksite(title,url)
{
	if (window.sidebar)
	{ // firefox
		window.sidebar.addPanel(title, url, "");
	}
	else if(window.opera && window.print)
	{ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
	else if(document.all)
	{// ie
		window.external.AddFavorite(url, title);
	}
}
function isNumber(e)
{
	if(e.which) // Netscape/Firefox/Opera
	{
		val = e.which
	}
	else if(window.event) // IE
	{
		val = e.keyCode;
	}
	if (val!=8)
	{ //if the key isn't the backspace key (which we should allow)
        if(val>47 && val<58)
            return true;
		else
			return false;	 //disable key press
	} //disable key press
}
function isPrice(e)
{
	if(e.which) // Netscape/Firefox/Opera
	{
		val = e.which
	}
	else if(window.event) // IE
	{
		val = e.keyCode;
	}
	if (val!=8)
	{ //if the key isn't the backspace key (which we should allow)
        if((val>47 && val<58) || val==46 || val==37 )
            return true;
		else
			return false;	 //disable key press
	} //disable ke
}
function checkemail(email)
{
	
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(email))
		return true;
	else
		return false;
}
//to get the text of selected index
function getText(code,textname)
{ 
	countryname=code.options[code.selectedIndex].text;
	document.getElementById(textname).value=countryname;
}
function removeSpaces(string) {
 return string.split(' ').join('');
}
function isSizeExceeded(val,e)
{
	if(e.which) // Netscape/Firefox/Opera
	{
		keyval = e.which
	}
	else if(window.event) // IE
	{
		keyval = e.keyCode;
	}
	if (keyval!=8)
	{ 
		if(val.length>50)
		{
			return false;
		}
		else
			return true;
	}
}

function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
  var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
  return newnumber;
}function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
function getComputerInfo(cartid,itemtype,price)
{
	xmlHttp=createRequestObject();
	if (xmlHttp==null)
	{
	alert ("Your browser does not support AJAX!");
	return;
	} 
	document.getElementById('img_'+cartid).style.display = ""; // line added by shiva
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState == 4)
		{
			if(xmlHttp.status == 200)
			{
				//alert(xmlHttp.responseText);
				document.getElementById('middlecontent').innerHTML=xmlHttp.responseText;
			}
		}
	}
	var url='computeritems.php?cartid='+cartid+'&itemtype='+itemtype+'&price='+price;
	xmlHttp.open("GET",url,true);	
	xmlHttp.send(null);
}

function validateAdmin()
{
	var flag=0;
	if(document.getElementById('adminname').value=='')
	{
		document.getElementById('adminname_err').innerHTML="Please Enter Name";
		flag=1;
	}
	else
	{
		document.getElementById('adminname_err').innerHTML="";
	}
	if(document.getElementById('adminpassword').value=='')
	{
		document.getElementById('adminpassword_err').innerHTML="Please Enter Password";
		document.getElementById("pass_match_err").innerHTML="";
		flag=1;
	}
	else
	{
		document.getElementById('adminpassword_err').innerHTML="";
	}
	if(document.getElementById('admincpass').value=='')
	{
		document.getElementById('admincpass_err').innerHTML="Please Enter Confirm Password";
		document.getElementById("pass_match_err").innerHTML="";
		flag=1;
	}
	else
	{
		document.getElementById('admincpass_err').innerHTML="";
	}
	
	if(document.getElementById("adminemail").value=="")
	{
		document.getElementById("adminemail_err").innerHTML="Please Enter Email";
		document.getElementById("adminemail_inval_err").innerHTML="";
		flag=1;
	}
	else if( document.getElementById("adminemail").value!="") 
	{
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var address = document.getElementById("adminemail").value;
		if(reg.test(address) == false)
		{
		  document.getElementById("adminemail_inval_err").innerHTML="Invalid Email";
		  document.getElementById("adminemail_err").innerHTML="";
		 flag=1;
		}
		else 
		{
		document.getElementById("adminemail_inval_err").innerHTML="";
		}
	}
	else
	{
			document.getElementById("adminemail_err").innerHTML="";
	}
	
	if((document.getElementById("adminpassword").value!='')&&(document.getElementById("admincpass").value!=''))
	{
		if(document.getElementById("adminpassword").value!=document.getElementById("admincpass").value)
		{
			document.getElementById("pass_match_err").innerHTML="Password doesn't Match";
			document.getElementById('admincpass_err').innerHTML="";
			document.getElementById('adminpassword_err').innerHTML="";
			flag=1;
		}
		else
		{
			document.getElementById("pass_match_err").innerHTML="";
		}
	}
	
	if(flag>0)
		return false;
}

function ValidatePassword()
{
	var flag=0;
	if(document.getElementById('password').value=='')
	{
		document.getElementById('password_error').innerHTML="Please Enter Password";
		document.getElementById("match_err").innerHTML="";
		flag=1;
	}
	else
	{
		document.getElementById('password_error').innerHTML="";
	}
	if(document.getElementById('retypepassword').value=='')
	{
		document.getElementById('retypepassword_err').innerHTML="Please Retype Password";
		document.getElementById("match_err").innerHTML="";
		flag=1;
	}
	else
	{
		document.getElementById('retypepassword_err').innerHTML="";
	}
	
	if((document.getElementById("password").value!='')&&(document.getElementById("retypepassword").value!=''))
	{
		if(document.getElementById("password").value!=document.getElementById("retypepassword").value)
		{
			document.getElementById("match_err").innerHTML="Password doesn't Match";
			document.getElementById('password_error').innerHTML="";
			document.getElementById('retypepassword_err').innerHTML="";
			flag=1;
		}
		else
		{
			document.getElementById("match_err").innerHTML="";
		}
	}
	
	if(flag>0)
		return false;
}
function setmystores()
{
	var checked = true;
	var milton=document.getElementById("mystore_milton");
	var rocklea=document.getElementById("mystore_rocklea");
	var goldcoast=document.getElementById("mystore_goldcoast");
	var sunshinecoast=document.getElementById("mystore_sunshinecoast");
	var milton_checked=0;
	var rocklea_checked=0;
	var goldcoast_checked=0;
	var sunshinecoast_checked=0;
	if(milton.checked == true)
	{
		milton_checked = 1;
		milton.checked = checked;
	}
	else
		milton.checked = false;
	if(rocklea.checked == true)
	{
		rocklea_checked = 1;
		rocklea.checked = checked;
	}
	else
		rocklea.checked = false;
	if(goldcoast.checked == true)
	{
		goldcoast_checked = 1;
		goldcoast.checked = checked;
	}
	else
		goldcoast.checked = false;
	if(sunshinecoast.checked == true)
	{
		sunshinecoast_checked = 1;
		sunshinecoast.checked = checked;
	}
	else
		sunshinecoast.checked = false;
	xmlHttp=createRequestObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	//computeritems.php?
	var url='setmystore.php?milton_checked='+milton_checked+'&rocklea_checked='+rocklea_checked+'&goldcoast_checked='+goldcoast_checked+'&sunshinecoast_checked='+sunshinecoast_checked;
	
	xmlHttp.open("GET",url,true);	
	xmlHttp.send(null);
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState == 4)
		{
			if(xmlHttp.status == 200)
			{
				//alert(xmlHttp.responseText);
				//document.getElementById('middlecontent').innerHTML=xmlHttp.responseText;
			}
		}
	}
}
function showmystores()
{
	document.getElementById("setmystore").style.display="block";
}
//used to test the query. displays products.
function ShowResults(headingid,orderby,divid)
{
	//alert(headingid);
	
	xmlHttp=createRequestObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState == 4)
		{
			if(xmlHttp.status == 200)
			{
				//alert(xmlHttp.responseText);
				document.getElementById('loader_'+divid).style.display = 'none';
				document.getElementById('results_'+divid).style.display = '';
				document.getElementById('results_'+divid).innerHTML=xmlHttp.responseText;
			}
		}
	}
	document.getElementById('loader_'+divid).style.display = '';
	var url='showresults.php?headingid='+headingid+'&orderby='+orderby+'&divid='+divid;
	xmlHttp.open("GET",url,true);	
	xmlHttp.send(null);
}
function CloseResults(divid)
{
	document.getElementById('results_'+divid).style.display = 'none';
	//document.getElementById('results_'+divid).innerHTML=xmlHttp.responseText;
}
