﻿// JScript File

function  validateUpload() 
{
    //alert("Hello!");
    var ddlLoc = document.getElementById(loc);
    var txtUpl = document.getElementById(upl);
    var errText = "";
 
    if (ddlLoc.value == -1)
        errText += "    Must select Category.\n"
    //if (!validateNotEmpty(txtFrom.value))
    //    errText += "    Start Date is missing.\n"
    //if (!validateNotEmpty(txtTo.value))
    //    errText += "    End Date is missing.\n" 
    if (!validateNotEmpty(txtUpl.value))
        errText += "    File is missing.\n"
    else 
    {
        if (!validateFileType(txtUpl.value))
      		errText += "	Invalid File Type \n"
	}	
	    
	if (errText.length > 0) 
    {
        alert("Your information could not be submitted because of the following:\n\n" 
           	  + errText + "\nPlease correct and resubmit.");
        return false;
    }
    //else if (isDate(txtFrom.value)==false || isDate(txtTo.value)==false)
	//		return false
			
    return true;
}

function validateSearch()
{
     //alert("Hello!");
    var txtFrom = document.getElementById(from);
    var txtTo = document.getElementById(to);
    var errText = "";

    if (validateNotEmpty(txtFrom.value))
    {
        if (isDate(txtFrom.value)==false)
			return false
    }
	 if (validateNotEmpty(txtTo.value))
    {
        if (isDate(txtTo.value)==false)
			return false
    }		
    return true;
}

function validateSendEmail()
{
    var txtFrom = document.getElementById(from);
    var txtTo = document.getElementById(to);
    var txtSubject = document.getElementById(subject);
    var errText = "";

	if (!validateNotEmpty(txtTo.value)) 		errText += "	Recipient e-mail address is missing.\n";
	if (!validateEmail(txtFrom.value))	        errText += "	Sender e-mail address is either missing or invalid.\n";
	if (!validateNotEmpty(txtSubject.value)) 	errText += "	Subject text is missing.\n";
									 
	if (errText.length > 0) 
	{
		alert("Your information could not be submitted because of the following reason(s):\n\n" 
   		+ errText + "\nPlease correct and resubmit.");
		return false;
	}
	return true;
}

function validateSearchXX()
{
     //alert("Hello!");
    var ddlLoc = document.getElementById(loc);
    var ddlPrice = document.getElementById(price);
    var txtFrom = document.getElementById(from);
    var txtTo = document.getElementById(to);
    var errText = "";
    
    if (ddlLoc.value == 0)
        errText += "    Must select Category.\n"
    if (ddlPrice.value == 0)
        errText += "    Must select Price Range.\n"
    
        
    if (errText.length > 0) 
    {
        alert("Your information could not be submitted because of the following:\n\n" 
           	  + errText + "\nPlease correct and resubmit.");
        return false;
    }
    if (validateNotEmpty(txtFrom.value) || validateNotEmpty(txtTo.value))
    {
        if (isDate(txtFrom.value)==false || isDate(txtTo.value)==false)
			return false
    }
			
    return true;
}

function validateFileType( strValue) 
{
	var objRegExp = /(\.xls$)|(\.csv$)/i;
	return objRegExp.test(strValue);
}

function validateEmail( strValue) 
{
	var objRegExp  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,4}(\.[a-z]{2}){0,2})$/i;
  	return objRegExp.test(strValue);
}
  		   				
function validateNotEmpty( strValue ) 
{
   	var strTemp = strValue;
   	strTemp = trimAll(strTemp);
   	if(strTemp.length > 0){
     	return true;
   	}  
   	return false;
}

function trimAll( strValue ) 
{

 var objRegExp = /^(\s*)$/;

    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }
    
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function confirmSubmit()
{
var agree=confirm("Are you sure you want to continue?");
if (agree)
	return true ;
else
	return false ;
}

function  validateNumeric( strValue ) {
  	var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
 
  	return objRegExp.test(strValue);
}

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)
{
	//alert("Hello")
	var frm = document.aspnetForm;
	var dtCh= "/";
	var minYear=1900;
	var maxYear=2100;

	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)

	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 (strYear.charAt(0)=="0" && strYear.length>1) strYear=strYear.substring(1)
	}

	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYear)

	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/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
	}
	//alert(" month = " + month + " day = " + day + " year = " + year);

	//var now = new Date();
	//now = now.getTime(); 
	//var dateToCheck = new Date();
	//dateToCheck.setYear(year);
	//dateToCheck.setMonth(month-1);
	//dateToCheck.setDate(day);
	
	//var checkDate = dateToCheck.getTime();
	//alert("now = " + now + " checkDate = " + checkDate)
	
	//if (now > checkDate)
	//{
	//	alert("Please enter a valid date")
	//	return false
	//}
    return true
}