//check valid dates
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function ValidateDate(dt){

	if (isDate(dt.value)==false){
		dt.focus();
		dt.select();
		return false;
	}
    return true
 }


//check valid numbers for phone,hp,fax,qty,total price, gst
function validate(field) {
var valid = "0123456789."
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Invalid entry!  Only numbers are accepted!");
field.focus();
field.select();
   }
}

// check login -->
function setColor(el, bg) {
  if (el.style) el.style.backgroundColor = bg;
}
function login() {
  var bgBad = "#cccc99";
  var bgGood = "white";
  var valid = true;
var themessage = "You are required to complete the following fields: ";
if (document.loginform.username.value=="") {
   themessage = themessage + " - Login";
   valid = false;
   setColor(document.loginform.username, bgBad);
} 
else {
    setColor(document.loginform.username, bgGood);
  }
if (document.loginform.password.value=="") {
   themessage = themessage + " -  Password";
   valid = false;
   setColor(document.loginform.password, bgBad);
} 
else {
    setColor(document.loginform.password, bgGood);
  }
//alert if fields are empty and cancel form submit
if (!valid){
  alert(themessage);
  return false;
}
}

//make sure all compusary fields are filled

  // This function returns true ONLY if the required field is set
  function isRequired(inputName){ 
     return ( (typeof(inputName.required) != 'undefined') && (inputName.required.toLowerCase() != 'false') ); 
  }

  // This function returns true if the field has no value
  function isBlank(inputName){ return (inputName.value == ""); }
    
  // This function highlights the input and returns false
  function highlightInput(inputName, message){
	var bgBad = "#cccc99";
    var bgGood = "white";
    if (message == null) { message = "One or more compulsory fields are required before submission." }
    alert(message);
    setColor(inputName,bgBad);
    inputName.focus();
    return false;
   }
   
function checkfields(formName){
  var bgBad = "#cccc99";
  var bgGood = "white";

  for(var i=0; i<formName.elements.length; i++)
  {
       var inputName = formName.elements[i];
       if(isRequired(inputName) && isBlank(inputName))
       { return highlightInput(inputName); }
	   else{
	      setColor(inputName,bgGood);
       }
  }
  return true;
}
//function to compute total price
function compute_price(){
  var bgBad = "#cccc99";
  var bgGood = "white";
  var qty=0, unit_price=0, gst=0, total_price=0;
  var form = document.form1;
  if(form.unit_price.value == "" || form.unit_price.value == "0"){
     alert ("You must key in the unit price");
	 setColor(form.unit_price,bgBad);
	 form.unit_price.focus();
	 return false;
  }
  else{
     setColor(form.unit_price,bgGood);
  }
  if(form.qty.value == "" || form.qty.value == "0"){
     alert ("You must key in the quantity");
	 setColor(form.qty,bgBad);
	 form.qty.focus();
	 return false;
  }
  else{
     setColor(form.qty,bgGood);
  }
  if(form.gst.value == "" || form.gst.value == "0"){
     alert ("You must key in the GST");
	 setColor(form.gst,bgBad);
	 form.gst.focus();
	 return false;
  }
  else{
     setColor(form.gst,bgGood);
  }
  unit_price = form.unit_price.value;
  gst = form.gst.value;
  qty = form.qty.value;
  
  total_price = (unit_price * qty) * (gst / 100);
  total_price = (unit_price * qty) + total_price;
  form.total_price.value = total_price;
  setColor(form.total_price,bgGood);
    
}

//double check price before final submission
function dctotalprice(form){
//  var form = document.form1;
  var qty,gst,unit_price,total;

  qty = form.qty.value;
  gst = form.gst.value;
  unit_price = form.unit_price.value;
   
  if(qty=="" || qty=="0" || gst=="" || gst=="0" ){
     alert("Error in calculating Total Price. Please check Unit Price, Qty and GST.");
	 return false;
  }
  else{
  total = (qty*unit_price) * (gst/100);
  total += (qty*unit_price);
  
   if(form.total_price.value != total){
      var ans = confirm("The total price does not tally with Quantity,Unit Price and GST. Are you sure you want to continue?");
	  if(ans){return true;}
	  else{
	  form.total_price.focus();
	  form.total_price.select();
	  return false;}
   }
   else
     return true;
  }

}  
//check duplicate files and invalid file formats
function checkForm(formName){
   var numberOfControls = formName.length;

   for (var controlIndex = 0; controlIndex < numberOfControls; controlIndex++){

       element = formName[controlIndex];
       if(element.type == "file" && (element.value!="" || element.value!=null)){
//  	      validformFile = /(.jpg|.JPG|.gif|.GIF)$/;
   	      validformFile = /(.exe|.EXE|.swf|.SWF|.ini|.INI.|.com|.COM|.bat|.BAT|.pif|.PIF|.dll|.DLL)$/;
          if(validformFile.test(element.value)){
              alert("Only NON-EXECUTABLE files are supported. Please select another file again.");
              element.focus();
			  element.select();
              return false;
          }
       }
   }
   return true;
}

function checkdup(formName){
  var valid=false;
  valid = checkForm(formName);
  if(valid){
      valid = checkfields(formName);
      if(valid){
         var valid2 = checkduplicate(formName);
           if(valid2){
            popup();
	        return true;
           }
	       else
	            return false;
      }
     else return false;
  }
  else return false;
}

function checkduplicate(formName){
   var numberOfControls = formName.length;
  
   for (var controlIndex = 0; controlIndex < numberOfControls; controlIndex++){
       element = formName[controlIndex];
       if(element.type == "file" && element.value!=""){
	       for (var controlIndexs = controlIndex+1; controlIndexs < numberOfControls; controlIndexs++){
		      element2 = formName[controlIndexs];
			  if(element2.type=="file" && element2.value!=""){
			      if(element.value==element2.value){
				        alert("Duplicate files found");
						element2.select();
						return false;
				  }
			  }
		  
		   }
       }
   }
   return true;
}
//update checkboxes for delete uses
function RecUpdate(RecID){
var ThisID = (RecID)
var controlIndex;
var element;
var numberOfControls = document.form1.length;

document.form1.hidRecIDs.value="";
for (controlIndex = 0; controlIndex < numberOfControls; controlIndex++){
   element = document.form1[controlIndex];
   if(element.type == "checkbox"){
		  if(element.checked == true){
                  if (document.form1.hidRecIDs.value == ""){
                        document.form1.hidRecIDs.value = element.value;
                   }
                  else{//if (document.form1.hidRecIDs.value != ""){
                        var str = document.form1.hidRecIDs.value;
                        //var pos = str.indexOf(element.value);
                        //if (pos == -1) { 
                            document.form1.hidRecIDs.value = document.form1.hidRecIDs.value + ", " + (element.value) 
                        //}
                  }
           }
   }
}

}

//popup progress indication window
var nWin; 

function popup(formName) { 
   var numberOfControls = formName.length;
   var winl = (screen.width - 300) / 2;
   var wint = (screen.height - 200) / 2;
   var winprops = 'height=200, width=300,top='+wint+',left='+winl+', scrollbars=No, resizable=No, status=No, toolbar=No, location=No';
   for (var controlIndex = 0; controlIndex < numberOfControls; controlIndex++){
       element = formName[controlIndex];
       if(element.type == "file" && element.value!=""){
             document.forms[0].submit(); 
             nWin=window.open("wait.htm","",winprops); 
             return;
       }
   }

} 

function closewin() { 

nWin.close(); 

} 
//open files in new window
function opennewwin(page){
OpenWin = window.open(page, "CtrlWindow", "height=600, width=600, toolbar=no,status=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
}

//delete job request
function updatedel(formName){
   var form = document.form2;
   if(form.delbox.checked)
      form.delbox.value = "Yes";
   else
      form.delbox.value ="No";
}

function gotomain(){
  // document.loginform.username.value = logid;
  // document.loginform.password.value = pwd;
   document.main.location.href = document.loginform.submit();
}
//  End -->