// JScript source code
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isBigInteger(s)		//this function is for check valid date only
{
	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 || isBigInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function isDate_withoutAlert(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){
		
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isBigInteger(stripCharsInBag(dtStr, dtCh))==false){
		
		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, check value < (2^15)-1 = 32767
    
    if(s > 32767)
    {
		return false;
    }    
    return true;
}

function isLong(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, check value < 2147483647
    if(s > 2147483647)
    {
		return false;
    }
    return true;
}

//-------------------------------------------------------------------
// isNumeric(value)
//   Returns true if value contains a positive float value
//-------------------------------------------------------------------
function isNumeric(val) 
{
	var dp = false;			//dp: decimal point
	for (var i=0; i < val.length; i++)		//begin at 0 
	{
		if (!isDigit(val.charAt(i))) 
		{ 
			if (val.charAt(i) == '.' & val.length > 2 & i > 0)	//not accept .0923 
			{
				if (dp == true || i == (val.length-1) ) { return false; } // already saw a decimal point or "." at the end of string
				else { dp = true; }
			}
			else 
			{
				return false; 
			}
		}
	}
	if(val > 1000000000000)
	{
		return false;
	}
	
		return true;
}

//-------------------------------------------------------------------
// tt050927
// roundNumber(value)
//   Round number to 2 digits after decimal point
//-------------------------------------------------------------------
function roundNumber(value) {
	if(value != '')
	{
		if(isNumeric(value))
		{ 
			var rlength = 2; // The number of decimal places to round to
			var newnumber = Math.round(value*Math.pow(10,rlength))/Math.pow(10,rlength);
			return newnumber;
		}
	}
	
	return value;
	
}



//-------------------------------------------------------------------
// isDigit(value)
//   Returns true if value is a 1-character digit
//-------------------------------------------------------------------
function isDigit(num) {
	var string="1234567890";
	if (string.indexOf(num) != -1) {
		return true;
		}
	return false;
	}

//-------------------------------------------------------------------
// isString(s)
//   Returns true if value is a valid string
//-------------------------------------------------------------------
function isString(s)
	{
	var i;
    for (i = 0; i < s.length; i++)
		{   
        // Check that current character is number.
        var c = s.charAt(i);
        if (c=='>' || c=='<' || c=='{' || c=='}') return false;
		}
	return true;
	}
	
//-------------------------------------------------------------------
// isCouponCode(s)
//   Returns true if value is a valid coupon code [050817]
//-------------------------------------------------------------------
function isCouponCode(s)
	{
	if(s.length != 8)
		{
		return false;
		}
	if(s.match(/^[A-Za-z0-9]*$/)==null) //tt: accept letters + digits, check using regexp
		{
		return false;
		}
	return true;
	}
	
//////////////////////////////
function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);
    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);
   return strText;
}

function SSLtrim(strText) { 
    //th:050921
    // this will get rid of leading spaces
    // and remove from strText the characters: !, #, $, %, &, *, ?, /, <, >
    // to solve shared SSL problem in H-Sphere
    //th:050921
   strLength = strText.length; 
   for (i = 0; i < strLength; i++)
   {  
		strText = strText.replace("!", "")
		strText = strText.replace("#", "")
		strText = strText.replace("$", "")
		strText = strText.replace("%", "")    
		strText = strText.replace("&", "")
		strText = strText.replace("*", "")
		strText = strText.replace("?", "")
		strText = strText.replace("/", "")
		strText = strText.replace("<", "")
		strText = strText.replace(">", "")
	}
    
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);
    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);    
    
   
   return strText;
}

function LTrim(str) {
	for (var i=0; str.charAt(i)==" "; i++);
	return str.substring(i,str.length);
	}
function RTrim(str) {
	for (var i=str.length-1; str.charAt(i)==" "; i--);
	return str.substring(0,i+1);
	}
function Trim1(str) {
	return LTrim(RTrim(str));
	}
///////////////////
function CheckEmail(field) {
    //field=CheckSpace(field);	
    var email="Please enter a valid e-mail address!\n\n"+field;
    var len=field.length;
    var bef=0,aft=0;
    i=0;  dem=0;
    while (field.indexOf('@',i)!=-1) 
          { i=field.indexOf('@',i)+1;
             dem++;
             if((field.indexOf('@',0)==0)||(field.indexOf('..',0)!=-1)) { window.alert(email);return false; }
          }
    if ((dem==0) || (dem>=2)) 
           { window.alert(email); return false;}
    else {  for(i=0;i<len;i++)
                   { var codeChr=field.charCodeAt(i);
                      if (((codeChr>=48) && (codeChr<=57)) || ((codeChr>=64) && (codeChr<=90)) || ((codeChr>=97)&& (codeChr<=122)) || (codeChr==46)||(codeChr==95))
                             {  bef+=1;
                                 if(codeChr==64) aft=0;
                                 else aft+=1;
                              }
                      else { window.alert(email); return false;}
                   }               
                if((bef<2)||(aft <4)) { window.alert(email);return false; }
                i=0;  dem=0;
                while (field.indexOf('.',i)!=-1) {i=field.indexOf('.',i)+1;dem++;}
                if (dem==0) { window.alert(email); return false;}
                if((field.indexOf('@.',0)!=-1)||(field.indexOf('.@',0)!=-1)) { window.alert(email);return false; }
            }
   return true;
}
////////////////////////
// Convert from dd/mm/yyyy to mm/dd/yyyy
function ConvertDate(dtStr){
	var dtCh= "/";	
			
	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)
		
		month=parseInt(strMonth)
		day=parseInt(strDay)
		year=parseInt(strYear)
	var strDateConverted = strMonth + '/' + strDay + '/' + strYear
	
	return strDateConverted
}

function Changetoyyyy(dtStr) //change from dd/mm/yy to dd/mm/yyyy
{
	var dtCh= "/";	
			
	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)
	if(isLong(strYear) && isLong(strMonth) && isLong(strDay) && strYear != '' && strMonth != ''  && strDay != '')
		{	
		if ((strYear.length ==2) && (Number(strYear) > 80) && (Number(strYear) < 100))
			{
			strYear = '19' + strYear;
			}
		if ((strYear.length ==2) && (Number(strYear) > 0) && (Number(strYear) < 80))
			{
			strYear = '20' + strYear;
			}
		var strDateChanged = strDay + '/' + strMonth + '/' + strYear
		return strDateChanged
		}
	return dtStr
}

function submitonce(theform)
	{
	// if IE 4+ or NS 6+
	if (document.all||document.getElementById)
		{
		//screen thru every element in the form, and hunt down "submit" and "reset"
		for (i=0;i<theform.length;i++)
			{
			var tempobj=theform.elements[i];
			if(tempobj.type.toLowerCase()=="submit" || tempobj.type.toLowerCase()=="image" || tempobj.type.toLowerCase()=="button" || tempobj.type.toLowerCase()=="reset")
				//disable em
				tempobj.disabled=true;
			}
		}
	}
function submitenabled(theform)
	{
	// if IE 4+ or NS 6+
	if (document.all||document.getElementById)
		{
		//screen thru every element in the form, and hunt down "submit" and "reset"
		for (i=0;i<theform.length;i++)
			{
			var tempobj=theform.elements[i];
			if(tempobj.type.toLowerCase()=="submit" || tempobj.type.toLowerCase()=="image" || tempobj.type.toLowerCase()=="button" || tempobj.type.toLowerCase()=="reset")
				//disable em
				tempobj.disabled=false;
			}
		}
	}