// JavaScript Document
// Common javascript functions.


//-------------------   Trim functions	----------------------------------//
function ltrim(string)
  {
    string=new String(string);
    var string1=new Array();
    var i,j;
    for(i=0,j=0;i<string.length;i++)
    {
      if(j==0)
      {
        if(string.charAt(i)!=" ")
        {
            string1[j++]=string.charAt(i);
        }
      }
      else
      {
        string1[j++]=string.charAt(i);
      }
        
    }
    string="";
    for(i=0;i<string1.length;i++)
    {
      string+=string1[i];
    } 
    return string;
}

function rtrim(string)
  {
    string=new String(string);
    var string1=new Array();
    var i,j;
    for(i=string.length;i>=0;i--)
    {
      if(string.charAt(i-1)==" ")
      {
        continue;
      }
      else
      {
        for(j=0;j<i;j++)
        {
          string1[j]=string.charAt(j);
        }
        break;
      }
        
    }
    string="";
    for(i=0;i<string1.length;i++)
    {
      string+=string1[i];
    }
    
    
    return string;
}
  
  function trim(string)
  { 
    string=ltrim(string); // // This function is used to trim the left side of a String
    string=rtrim(string);// This function is used to trim the right side of a String
    return string;
  }
  
  //----------------------------------- Trim function ends ---------------------------------//
  
 
  
  
//----------------Function for date difference from current date ------------------------//

function funDateDifference(myDate)
{
	splitarr=myDate.split("/");
	month=splitarr[0]-1;
	day=splitarr[1];
	year=splitarr[2];

		var eventDate =new Date(year, month, day) //Month is 0-11 in JavaScript
		today=new Date()
		//Get 1 day in milliseconds
		var one_day=1000*60*60*24
		
		diffInDays=Math.ceil((eventDate.getTime()-today.getTime())/(one_day));
		
		return diffInDays;
}
//----------------Function  ends here ------------------------//

	var email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
	var phone=/(^\d{3}-\d{3}-\d{4}$)/;
	var dateExp=/(^\d{2}[//]\d{2}[//]\d{4}$)/
	var numberExp = /^([0123456789.])+$/;
		
function isDate(regDate)
{
	if(!dateExp.test(regDate))
	{
		alert("Please enter a valid date in the format(mm/dd/yyyy).");		
		return false;
	}
	else if(parseInt(regDate.substr(0,2))>12)
	{
		alert("Please enter valid month.");		
		return false;
	}
	else if(parseInt(regDate.substr(3,2))>31)
	{	
		alert("Please enter valid day.");		
		return false;
	}
	else if(parseInt(regDate.substr(6,4))<2000 || parseInt(regDate.substr(6,4))>2100)
	{	
		alert("Please enter valid year.");		
		return false;
	}
	else
		return true;
}

//----------------Function for  finding the difference of two given dates --------//

function checkDateDifference(lowDate, highDate, comparison) 
{
	lowDateSplit = lowDate.split('/');
	highDateSplit = highDate.split('/');

	date1 = new Date();
	date2 = new Date();

	date1.setDate(lowDateSplit[0]);
	date1.setMonth(lowDateSplit[1] - 1);
	date1.setYear(lowDateSplit[2]);

	date2.setDate(highDateSplit[0]);
	date2.setMonth(highDateSplit[1] - 1);
	date2.setYear(highDateSplit[2]);

	if(comparison == "eq") {
		if(date1.getTime() == date2.getTime()) {
			return true;
		}
		else {
			return false;
		}
	}
	else if(comparison == "lt") {
		if(date1.getTime() < date2.getTime()) {
			return true;
		}
		else {
			return false;
		}
	}
	else if(comparison == "gt") {
		if(date1.getTime() > date2.getTime()) {
			return true;
		}
		else {
			return false;
		}
	}
	else if(comparison == "le") {
		if(date1.getTime() <= date2.getTime()) {
			return true;
		}
		else {
			return false;
		}
	}
	else if(comparison == "ge") {
		if(date1.getTime() >= date2.getTime()) {
			return true;
		}
		else {
			return false;
		}
	}
}


function DateDiff(t1,t2)
{
	//Total time for one day
    var one_day=1000*60*60*24; 
		
	var x=t1.split("/");     
	var y=t2.split("/");
	//date format(Fullyear,month,date) 

	var date1=new Date(x[2],(x[1]-1),x[0]);

	var date2=new Date(y[2],(y[1]-1),y[0])
	var month1=x[1]-1;
	var month2=y[1]-1;
	
	//Calculate difference between the two dates, and convert to days
		   
	_Diff=Math.ceil((date2.getTime()-date1.getTime())/(one_day)); 
	//alert(_Diff);
	
}

function timeDifference(laterdate1,earlierdate1) {
	
	laterDate1=laterdate1.split("/");//alert(laterDate1[1]);
	earlierDate1 = earlierdate1.split("/");
	
	var laterdate = new Date(laterDate1[2],laterDate1[0],laterDate1[1]);     // 1st January 2000
	var earlierdate = new Date(earlierDate1[2],earlierDate1[0],earlierDate1[1]);  // 13th March 1998

    var difference = laterdate.getTime() - earlierdate.getTime();

    var daysDifference = Math.floor(difference/1000/60/60/24);
    difference -= daysDifference*1000*60*60*24
    var hoursDifference = Math.floor(difference/1000/60/60);
    difference -= hoursDifference*1000*60*60
    var minutesDifference = Math.floor(difference/1000/60);
    difference -= minutesDifference*1000*60
    var secondsDifference = Math.floor(difference/1000);
	
	return daysDifference;
    //document.write('difference = ' + daysDifference + ' day/s ' + hoursDifference + ' hour/s ' + minutesDifference + ' minute/s ' + secondsDifference + ' second/s ');
}

//----------------  Function  ends here ------------------------//
// Declaring valid date character, minimum year and maximum year
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 strMonth=dtStr.substring(0,pos1)
	var strDay=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 : 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
	}
return true
}




//----------------Function for Validating Credit card ------------------------//


function validateCreditCard(cardType,cardNum)
{
				
	switch(cardType)
	{
		case 1://Visa  4111 1111 1111 1111 
				if(cardNum.charAt(0)!=4 || cardNum.charAt(1)!=1)
				{
					alert("Invalid VISA card number.");
					document.frm.txtcardnumber.value="";
					document.frm.txtcardnumber.focus();
					return false;
				}
				else 
				{
					return true;
				}
				break;
				
		case 2: 	//MasterCard  5500 0000 0000 0004 
				if(cardNum.charAt(0)!=5 || cardNum.charAt(1)!=5)
				{
					alert("Invalid MasterCard card number.");
					document.frm.txtcardnumber.value="";
					document.frm.txtcardnumber.focus();
					return false;
				}
				else 
				{
					return true;
				}


				break;
		case 3:  	//American Express  3400 0000 0000 009 
				if(cardNum.charAt(0)!=3 || cardNum.charAt(1)!=4)
				{
					alert("Invalid American Express card number.");
					document.frm.txtcardnumber.value="";
					document.frm.txtcardnumber.focus();
					return false;
				}
				else 
				{
					return true;
				}
				break;
		default: alert("Invalid Card type");
				break;
	}
}

//-------------- Credit card Validation ends here -------------------------------------//



//-------------------------------------------------------------------------------//
//---------------------- function to set  cookie for remember me option  ------- //

function set()
{
var name=document.frmlogin.txtemail.value;
var value=document.frmlogin.txtpassword.value;
var days=20;
createCookie(name,value,days);
	
}
function check(name)
{
	var pass=readCookie(name);
	if(pass!=null)
	{
		document.frmlogin.txtpassword.value=pass;
	}
}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) 
		return c.substring(nameEQ.length,c.length);
		//alert(c.substring(nameEQ.length,c.length));
	}
	return null;
}

function eraseCookie(name)
{	
	createCookie(name,"",-1);
}

//-------------------------------------------------------------------------------//
  
//-------------------- Function to  check the image is jpg or jpeg	-------------------------//
  function checkImage(theImage)
  {
		var ext1 = theImage.indexOf('.jpg');
		var ext2 = theImage.indexOf('.jpeg');
		var ext3 = theImage.indexOf('.JPG');
		var ext4 = theImage.indexOf('.JPEG');
		var ext5 = theImage.indexOf('.PJPEG');
		var ext6 = theImage.indexOf('.pjpeg');
		
		if (ext1 <= 0 & ext2 <= 0 & ext3 <= 0 & ext4 <= 0 & ext5 <= 0 & ext6 <= 0) 
		{
			alert("Please upload a JPEG image.");
			return false;
		} 
				
  }
//-------------------- Function checkImage ends 	---------------------------//

//------------- Function to  check whether the selected image is a valid one	------------------//

function imageValidation(fieldnameobj)
{
	
	if((fieldnameobj.value.lastIndexOf(".jpg")==-1)&&(fieldnameobj.value.lastIndexOf(".bmp")==-1)&&(fieldnameobj.value.lastIndexOf(".gif")==-1))
		{
		if((fieldnameobj.value.lastIndexOf(".JPG")==-1)&&(fieldnameobj.value.lastIndexOf(".BMP")==-1)&&(fieldnameobj.value.lastIndexOf(".GIF")==-1))
		{
	if((fieldnameobj.value.lastIndexOf(".Jpg")==-1)&&(fieldnameobj.value.lastIndexOf(".Bmp")==-1)&&(fieldnameobj.value.lastIndexOf(".Gif")==-1))
		{
			return false;
		}
	}
	}
return true;
}

//-------------------- Function imageValidation ends 	---------------------------//


//-------------------- Function to  check the entered text contains valid chars.------------------//
function checkchar(val)
 {
		// alert(val);
	   var flag=0;
	   var str 

	   str = val;
	  // var Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789#//%\\@._-+*',: ";
	  var Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/\@._-$ ";
	   for (var i = 0; i < str.length; i++)
           {
		if (Chars.indexOf(str.charAt(i)) == -1)
		{
			flag=1;	          
		}
           }

	   if(flag==1)
	   {
	        return false;
	   }
	   else
	   {
		return true;
	   }
 }
 //-------------------- Function checkchar ends 	---------------------------//
 
 //--------------- Function to  check the entered data is in a price format-------------------------//
 
  function checkprice(val)
 {
		// alert(val);
	   var flag=0;
	   var str 
       var dotcount=0;
	   str = val;
	   var Chars = "0123456789.,";
	   for (var i = 0; i < str.length; i++)
       {
		    if (Chars.indexOf(str.charAt(i)) == -1)
		    {
			    flag=1;	          
		    }
           if(str.charAt(i)==".")
		    {
		        dotcount=dotcount+1;
		    }
	    }
       if(dotcount>1)
       {
            return false;
       }
	   if(flag==1)
	   {
	        return false;
	   }
	   else
	   {
		return true;
	   }
 }
  //-------------------- Function checkprice ends 	---------------------------//
  
   //-------------------- Function to  check the entered data is an integer-------------------------//
  
function checkint(val)
 {
		// alert(val);
	   var flag=0;
	   var str 

	   str = val;
	   var Chars = "0123456789";
	   for (var i = 0; i < str.length; i++)
       {
		    if (Chars.indexOf(str.charAt(i)) == -1)
		    {
			    flag=1;	          
		    }
        }

	   if(flag==1)
	   {
	        return false;
	   }
	   else
	   {
		return true;
	   }
 }
 
//-------------------- Function checkprice ends 	---------------------------//
   
//--------------------Function to  check the entered data is in a phone no format -------------------------//
   
 function checkphone(val)
{
	var flag=0;
	var str ;
	
	str = val;
	var Chars = "0123456789-+/ ";
	for (var i = 0; i < str.length; i++)
	{
		if (Chars.indexOf(str.charAt(i)) == -1)
		{
			flag=1;	          
		}
	}
	
	if(flag==1)
	{
		return false;
	}
	else
	{
		return true;
	}
}
   
//-------------------- Function check taxid ends 	---------------------------//
   
 function checktaxid(val)
{
	var flag=0;
	var str ;
	
	str = val;
	var Chars = "0123456789-";
	for (var i = 0; i < str.length; i++)
	{
		if (Chars.indexOf(str.charAt(i)) == -1)
		{
			flag=1;	          
		}
	}
	
	if(flag==1)
	{
		return false;
	}
	else
	{
		return true;
	}
}
   
//-------------------- Function check taxid ends 	---------------------------//
//--------------------Function to get all the elements from a form -------------------------//


function GetElementName()
 {
     for(var i=0;i<document.forms[0].elements.length;i++)
	    {
		     alert(document.forms[0].elements[i].name);
		}
 }
 
 
 //-------------------- Function to check an email id 	---------------------------//
 
 function isValidEmailid(val)
{
	
        var email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
        if(!email.test(val))
            {
                return false;
            }
  
    return true;
}
 
 //-------------------- Function isValidEmailid ends 	---------------------------//
 
 
 
 

   
   //function ScrollIt(){
//    window.scrollTo(document.Form1.PageX.value, document.Form1.PageY.value);
//    }
//function setcoords(){
//    var myPageX;
//    var myPageY;
//    if (document.all){
//        myPageX = document.body.scrollLeft;
//        myPageY = document.body.scrollTop;
//        }
//    else{
//        myPageX = window.pageXOffset;
//        myPageY = window.pageYOffset;
//        }
//    document.Form1.PageX.value = myPageX;
//    document.Form1.PageY.value = myPageY;
//    }

//....................Function to check URL .............................//
 function checkurl(val)
 {
	 var url=/^([http:\/\/])+([www])+\.(([A-Za-z0-9-])+\.)+([a-zA-Z])+$/;
	 if(!url.test(val))
	{
		return false;
	}
	else
	{
		return true;
	}
 
 }

 //....................Function to check phone format......................//
 
function phchk(val)
{
	var exps=/(^\d{3}-\d{3}-\d{4}$)/;
	if(!exps.test(val))
	{
		return false;
	}
	else
	{
		return true;
	}
}
//..........................................................................//

 //....................Function to check TAX ID NUMBER format......................//
 
function taxidchk(val)
{ 
	var exps=/(^\d{2}-\d{10}-\d{1}$)/;
	if(!exps.test(val))
	{
		return false;
	}
	else
	{
		return true;
	}
}
//..........................................................................//

function timeCheck(val)
{
	var flag=0;
	var chr="0123456789: ";
	var str;
	str=val;
	
	for(var i=0;i<str.length;i++)
	{
		if(chr.indexOf(str.charAt(i))==-1)
		{
			flag=1;
		}
	}
	if(flag==1)
	{
		return false;
	}
	else
	{
		return true;
	}

}

/*--------Ajax Function------*/
function createObj()
{
	var request = false;
   try 
   {
     request = new XMLHttpRequest();
   } 
   catch (trymicrosoft)
   {
     try
	 {
       request = new ActiveXObject("Msxml2.XMLHTTP");
     }
	 catch (othermicrosoft) 
	 {
       try 
	   {
         request = new ActiveXObject("Microsoft.XMLHTTP");
       } 
	   catch (failed) 
	   {
         request = false;
       }  
     }
   }
	return request;
}

//-------- Function to  check the entered data contains characters '@ < > ( )' ------------//
  
function checksplchars(val)
 {
	   var flag=0;
	   var str 

	   str = val;
	   var Chars = "@<>()";
	   for (var i = 0; i < str.length; i++)
       {
		    if (Chars.indexOf(str.charAt(i)) != -1)
		    {
			    flag=1;	          
		    }
        }

	   if(flag==1)
	   {
	        return false;
	   }
	   else
	   {
		return true;
	   }
 }
 	/*for(i=0;i<document.frmforgotPwd.elements.length;i++)
	{
		alert(document.frmforgotPwd.elements[i].name);
	}*/
	
	
//---------------Date Validation---------//


//------- Declaring valid date character, minimum year and maximum year
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 pickRandom(range) 
{ 
	if (Math.random) return Math.round(Math.random() * (range-1)); 
	else 
	{ 
		var now = new Date(); 
		return (now.getTime() / 1000) % range; 
	} 
} 
