var dtCh= "/";
var minYear=2006;
var maxYear=2100;

function roll(img_name, img_src)
{
	document.getElementById(img_name).src = img_src;
}

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 mySubmit(){
//alert('submitting');
  if(validateWebForm()){
  	document.Quoteform.submit();
  }
  return false;
}

function openNew(url, name) {
 window.open(url,name);
}

function validateWebForm() {
    var valid = false;
    for (var i = 0; i<document.Quoteform.elements.length; i++) {
       if(document.Quoteform.elements[i].id != ''){
        if ((document.Quoteform.elements[i].id.indexOf('fn.') > -1)) {
			if(document.Quoteform.elements[i].value != ''){
				valid = true;
				break;
			}
        }
       }
    }
    if(document.Quoteform.cubic_metres.value != ''){
		valid = true;
    }
    if(document.Quoteform.other_items.value != ''){
		valid = true;
    }
    if(!valid){
		alert('Please enter inventory information before pressing submit');
    }else{
	  valid = validateDates();
    }
    return valid;
}

function validateDates(){
	var dp=document.Quoteform.date_of_pickup
	var dd=document.Quoteform.date_of_delivery
	var dpd = isDate(dp.value);
	if (dpd==false){
		dp.focus()
		return false
	}
	var ddd = isDate(dd.value);
	if (ddd==false){
		dd.focus()
		return false
	}
	if((document.Quoteform.date_of_pickup.value!='')&&(document.Quoteform.date_of_delivery.value!='')&&(ddd<dpd)){
		alert('Delivery date is before pickup date.');
		dp.focus()
		return false
	}
	var today = new Date();
	if((document.Quoteform.date_of_pickup.value!='')&&(dpd<today)){
		alert('Pickup date is in the past.');
		dp.focus()
		return false
	}
	if((document.Quoteform.date_of_delivery.value!='')&&(ddd<today)){
		alert('Delivery date is in the past.');
		dp.focus()
		return false
	}
    return true
}


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){
	if(dtStr == ''){
		return true
	}
	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("Please enter a valid date in dd/mm/yyyy format\nYou may use the dicker picker control by selecting the button beside the date field")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid date in dd/mm/yyyy format\nYou may use the dicker picker control by selecting the button beside the date field")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid date in dd/mm/yyyy format\nYou may use the dicker picker control by selecting the button beside the date field")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter valid 4 digit year\nYou may use the dicker picker control by selecting the button beside the date field")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date in dd/mm/yyyy format\nYou may use the dicker picker control by selecting the button beside the date field")
		return false
	}
	var resDate = new Date();
	resDate.setDate(day);
	resDate.setMonth(month-1);
	resDate.setFullYear(year);
	return resDate;
}


function checkrequirements(which, fields){
	var pass=true;
	if (document.images){
		for (i=0;i<which.length;i++){
			var tempobj=which.elements[i]
			if (isRequired(fields,tempobj.name)){
				if ((tempobj.type=="text"||tempobj.type=="textarea")&&tempobj.value==''){
					pass=false
					break
				}else if (tempobj.type.toString().charAt(0)=='s'){
					if(tempobj.selectedIndex==0){
						pass=false
						break
					}
				}

			}
		}
	}
	if (!pass){
		alert("One or more of the required fields have not been completed. Please complete them, then submit again!")
		return false
	}
	else{
		return true;
	}
}

function isRequired(arr, name){
	for(var i=0;i<arr.length;i++){
		if(arr[i] == name){
			return true;
		}
	}
	return false;
}

function validateDate(datefield){
	var	idDate = document.getElementById(datefield);
	var userFullDate = trim(idDate.value);
	
	var validcontent = /[0-3][0-9]\/[0-1][0-9]\/[0-9][0-9][0-9][0-9]|[0-9]\/[0-9]\/[0-9][0-9][0-9][0-9]/;
	if (!validcontent.test(userFullDate)) {	
		alert("Please enter the date in DD/MM/YYYY format");
		idDate.focus();
		return false;				
 	}		
	var userDateArray = userFullDate.split("/");
	var userYear  = userDateArray[2];   
	var userMonth = userDateArray[1];
	var userDay   = userDateArray[0];
	var currentTime = new Date();
	var month = currentTime.getMonth() + 1;
	var day = currentTime.getDate();
	var year = currentTime.getFullYear();
	if(userMonth > 12 || userDay > 31 ){
		alert("Please enter a valid date.");
		idDate.focus();
		return false;		
	}	
	if(userYear < year ||(userYear == year && userMonth < month)||(userYear == year && userMonth == month && userDay <= day)){
		alert("The move date should be a future date.");
		idDate.focus();
		return false;			
	}	
	return true;
}

function trim(str){
    if(!str || typeof str != 'string')
        return null;

    return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
}

