/*
* This file is used for common javascript utility functions
*/

/*
* getLength is used to return the length of the input string
* if input is null will return 0
* if ltrim flag is set, length will be returned after doing lefttrim on the input
* if rtrim flag is set, length will be returned after doing righttrim on the input
* if both flag are set, length will be returned after doing trim on the input
*/
function getLength(input,do_ltrim,do_rtrim)
{
	if(input==null || input=="")
	{
		return 0;
	}

	var tmpString= input;
	if(do_ltrim && !do_rtrim)
	{  
		tmpString= this.leftTrim(tmpString);
	}
	if(do_rtrim && !do_ltrim)
	{  
		tmpString= this.rightTrim(tmpString);
	}
	if (do_ltrim & do_rtrim)
	{
		tmpString= this.trim(tmpString);
	}
	return tmpString.length;
}

/*
* leftTrim is used to trim leading spaces of the input string
* if input is null will return ""
*/
function leftTrim(input)
{
	if(input==null || input=="")
	{
		return "";
	} 

	var objRegExp = /^(\s*)(\b[\w\W]*)$/;
    if(objRegExp.test(input)) 
	{
		input = input.replace(objRegExp, '$2');
	}
	return input;
}

/*
* rightTrim is used to trim trailing spaces of the input string
* if input is null will return ""
*/
function rightTrim(input)
{
	if(input==null || input=="")
	{
		return "";
	} 

	var objRegExp = /^([\w\W]*)(\b\s*)$/;
    if(objRegExp.test(input))
	{
       input = input.replace(objRegExp, '$1');
    }
    return input;
}

/*
* trim is used to trim leading spaces and trailing spaces of the input string
* if input is null will return ""
*/
function trim(input)
{
	if(input==null || input=="")
	{
		return "";
	} 

	var objRegExp = /^(\s*)$/;
    //check for all spaces
    if(objRegExp.test(input)) 
	{
       input = input.replace(objRegExp, '');
       if( input.length == 0)
          return input;
    }

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(input)) 
   {
       input = input.replace(objRegExp, '$2');
    }
	return input;
}

/*
* checkInteger is used to check if the input is an integer value
* if input is null or empty will return false
* if allowNegative flag is set, negative values will also be considered valid
*/
function checkInteger(input,allowNegative)
{
	if(input==null || input=="")
	{
		return false;
	} 

	if(!allowNegative)
	{
		var objRegExp  = /(^\d\d*$)/;
	}
	else
	{
		var objRegExp  = /(^-?\d\d*$)/; 
	}
	return objRegExp.test(input);
}

/*
* checkNumeric is used to check if the input is a numeric value
* if input is null or empty will return false
* if allowNegative flag is set, negative values will also be considered valid
*/
function checkNumeric(input,allowNegative)
{
	if(input==null || input=="")
	{
		return false;
	} 

	if(!allowNegative)
	{
		var objRegExp  =  /(^\d\d*\.\d*$)|(^\d\d*$)|(^\.\d\d*$)/;
	}
	else
	{
		var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
	}
	return objRegExp.test(input);
}

/*
* checkAlphabetical is used to check if the input consists only of alphabets(english)
* if input is null or empty will return false
*/
function checkAlphabetical(input)
{
	if(input==null || input=="")
	{
		return false;
	} 

	var objRegExp  =  /(^[a-zA-Z]+$)/; 
	return objRegExp.test(input);
}

/*
* checkAlphaNumeric is used to check if the input is an alphanumeric(english) value
* if input is null or empty will return false
*/
function checkAlphaNumeric(input)
{
	if(input==null || input=="")
	{
		return false;
	} 

	var objRegExp  =  /(^[a-zA-Z0-9]+$)/; 
	return objRegExp.test(input);
}

/*
* checkSplCharacters is used to check if the input contains special characters as specified by splChars
* if input is null or empty will return false
* if input contains splCharacters (example:!@#$%) will return true
* the special character list has to be enclosed in []+
*/
function checkSplCharacters(input,splChars)
{
	if(input==null || input=="")
	{
		return false;
	} 

	var objRegExp =  new RegExp(splChars);
	return objRegExp.test(input);
}

/*
* checkWhiteSpace is used to check if the input contains spaces
* if input is null or empty will return false
* if input contains space will return true
*/
function checkWhiteSpace(input)
{
	if(input==null || input=="")
	{
		return false;
	} 

	var objRegExp = /^.*[\s]+.*$/;
	return objRegExp.test(input);
}


/*
* checkEmail is used to check if the input is a valid email address
* if input is null or empty will return false
*/
function checkEmail(input)
{
	if(input==null || input=="")
	{
		return false;
	} 

	var objRegExp =  /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]*\.)+[a-zA-Z]{2,9})$/;
	return objRegExp.test(input);
}

/*
* checkExtDomain is used to check if the input is a valid external domain name
* if input is null or empty will return false
*/
function checkExtDomain(input)
{
	if(input==null || input=="")
	{
		return false;
	} 

	var objRegExp =  /(^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$)/i;
	return objRegExp.test(input);
}

/*
* checkLocalDomain is used to check if the input is a valid local domain name
* if input is null or empty will return false
*/
function checkLocalDomain(input)
{
	if(input==null || input=="")
	{
		return false;
	} 

	if(!checkWhiteSpace(input) && !(checkSplCharacters(input,"[*?!|\/'\"<>;,^()$~]+")))
	{
		return true;
	}
	return false;
}

/*
* preCheckExternalUrl is used to to build a valid url from checkExternalUrl
* returns input as is or modified input
*/
function preCheckExternalUrl(input)
{
	if(input!=null && input!="")
	{
		var flag= false;
		var tmpString1= input.substring(0,7).toLowerCase();
		var tmpString2= input.substring(0,8).toLowerCase();
		if(tmpString1=="http://" || tmpString2=="https://")
		{
			flag= true;
		}
		if(!flag)
		{
			input="http://"+input;
		}
	} 
	return input;
}

/*
* checkExternalUrl is used to check if the input is a valid external url
* if input is null or empty will return false
*/
function checkExternalUrl(input)
{    
	if(input==null || input=="")
	{
		return false;
	}
	var checkedInput= preCheckExternalUrl(input);
	var objRegExp =  /^(http:\/\/www.|https:\/\/www.|http:\/\/|https:\/\/){1}(?=[0-9a-zA-Z]+)/;
	if(objRegExp.test(checkedInput))
	{
			objRegExp =  /\.com|\.net|\.org|\.us|\.eu|\.biz|\.in|\.co.\in$/;
			return objRegExp.test(checkedInput);
	}
	else
	{
		return false;
	}
}

/*
* checkDateStrict is used to check if the input is a valid date between 1970-2038
* 1970 is javascript date limit and 2038 is php date limit
* if input is null or empty will return false
*/
function checkDateStrict(input)
{
	if(input==null || input=="")
	{
		return false;
	} 

	var objRegExp =  /(^(1[0-2]|0?[1-9])[\/](0?[1-9]|[12][0-9]|3[01])[\/]((19[789][0-9]|20([012][0-9]|3[0-8]))|([789][0-9]|([012][0-9]|3[0-8])))$)/;
	return objRegExp.test(input);
}

/*
* checkDate is used to check if the input is a valid date
* Will return true for any date between 1900-2099 for YYYY format
* Will return true for any date between 00-99 for YY format
* if input is null or empty will return false
*/
function checkDate(input)
{
	if(input==null || input=="")
	{
		return false;
	} 

	var objRegExp =  /(^(1[0-2]|0?[1-9])[\/](0?[1-9]|[12][0-9]|3[01])[\/]((19|20)[0-9][0-9]|[0-9][0-9])$)/;
	return objRegExp.test(input);
}

/*
* checkDateWithCurrDate is used to check if the input is a valid date with respect to current date
* internally calls checkDate to check input
* if equalTo flag is not set comparison will be only greater than current date 
* if input is null or empty will return false
*/
function checkDateWithCurrDate(input,inputTZ,dstFlag,hasTime,equalTo)
{
	//inputTZ,dstFlag and hasTime for future use
	if(input==null || input=="")
	{
		return false;
	} 

	if(checkDateStrict(input))
	{
		var curdate= new Date();

		var specdate= new Date(input);
		//check for yyyy year
		(parseInt(specdate.getFullYear()))<2000?(specdate.setFullYear(specdate.getFullYear()+100)):"";
		//add time component
		specdate.setHours(23);
		specdate.setMinutes(59);
		specdate.setSeconds(59);
		specdate.setMilliseconds(999);

		return (equalTo==true)?(specdate.getTime()>=curdate.getTime()):(specdate.getTime()>curdate.getTime());
	}
	else
	{
		return false;
	}
}

/*
* compareDates is used to check if the first date is equal to or greater than the second date
* internally calls checkDate to check input1 and input2
* call checkDateStrict on check input1 and input2 prior to this if date range check is required
* if equalTo flag is not set comparison will be only input2 greater than input1
* if input is null or empty will return false
*/
function compareDates(input1,input1TZ,dstFlag1,input2,input2TZ,dstFlag2,hasTime,equalTo)
{
	//inputTZ,dstFlag and hasTime for future use
	if((input1==null || input1=="") || (input2==null || input2=="") )
	{
		return false;
	} 

	if(checkDate(input1) && checkDate(input2))
	{
		var date1= new Date(input1);
		var date2= new Date(input2);
		//check for yyyy year
		(parseInt(date1.getFullYear()))<2000?(date1.setFullYear(date1.getFullYear()+100)):"";
		(parseInt(date2.getFullYear()))<2000?(date2.setFullYear(date2.getFullYear()+100)):"";
		return (equalTo==true)?(date2.getTime()>=date1.getTime()):(date2.getTime()>date1.getTime());
	}
	else
	{
		return false;
	}
}

/*
* checkCCard is used to check if the input could be a valid credit card number
* please note the use of could be in the above statement-- server side validation has to be done
* cardType refers to the card type-- id of the card provider
* if input is null or empty will return false
*/
function checkCCard(input,cardType)
{
	if(input==null || input=="")
	{
		return false;
	} 
	var ccardlength= getLength(input,true,true);
	var cardInput= "";
	if(!checkWhiteSpace(input) && checkInteger(input,false) && checkInteger(cardType,false))
	{   	
		cardInput= trim(input);
		switch(parseInt(cardType))
		{
			case 1: //amex
				if(ccardlength!=15)
				{
					return false;
				}
				var objRegExp= /^3(4|7)/;
				if(!objRegExp.test(input))
				{   
					return false;
				}
			break;
			case 2: //discover
				if(ccardlength!=16)
				{
					return false;
				}
				var objRegExp= /^6011/;
				if(!objRegExp.test(input))
				{
					return false;
				}
			break;
			case 3: //mastercard
				if(ccardlength!=16)
				{
					return false;
				}
				var objRegExp= /^5[1-5]/;
				if(!objRegExp.test(input))
				{
					return false;
				}
			break;
			case 4: //visa
				if(ccardlength==13 | ccardlength==16){}
				else
				{
					return false;
				}
				
				var objRegExp= /^4/;
				if(!objRegExp.test(input))
				{
					return false;
				}
			break;
			default: //mastercard
				return false;
			break;
		}
		//Luhn formula- mod10
		var checkSumTotal = 0;
		var checkSumTotal1 = 0;
		var checkSumTotal2 = 0;
		var checkSumTotal3 = 0;
		for(var a=ccardlength-1;((ccardlength%2==0)?a>0:a>=0);a=a-2)
		{   
			checkSumTotal1+= parseInt(cardInput.charAt(a));
			
		}
		for(var b=ccardlength-1;b>0;b=b-2)
		{   
			checkSumTotal2= String((cardInput.charAt(b-1)*2));
			for(var c=0;c<checkSumTotal2.length;c++)
			{
				checkSumTotal3+= parseInt(checkSumTotal2.charAt(c));
			}
		}
		checkSumTotal = checkSumTotal1+checkSumTotal3;
		return (checkSumTotal % 10 == 0);
		
	}
	else
	{
		return false;
	}
}
function stripHtmlContent(tempValue){
	var messageTemp=tempValue;
	if(messageTemp!=null && messageTemp!="" && getLength(messageTemp,true,true)>0){
		messageTemp=messageTemp.replace(/(<([^>]+)>)/ig,""); 
		messageTemp=messageTemp.replace(/(&nbsp;)/ig,"");
		return messageTemp;
	}else{
		return messageTemp;
	}
}
function phoneNumber(input)
{	

	if(input==null || input=="")
	{
		return false;
	} 		
		
		var objRegExp=null;
		var position=input.indexOf("(");
		var position=input.indexOf(")");
		
		if(position!=-1)
		{		
			objRegExp = /(^[^1]?\(\d{3}\)[-]?\d{3}[-]?\d{4}$)/ ;
		}
		else
		{
			objRegExp = /(^[^1]\(?\d{2}\)?[-]?\d{3}[-]?\d{4}$)/ ;
		}	
	
	return objRegExp.test(input);
}
function zipcode(input)
{	
	if(input==null || input=="")
	{
		return false;
	} 		
	
		var objRegExp=null;
		var position=input.indexOf("-");
		
		if(position!=-1)
		{			
			objRegExp = /(^\d{5}-\d{4}$)/ ;		
		}
		else
		{			
			objRegExp = /^((\d{5}\s\d{4})|(\d{5}))$/ ;			
		}	
	return objRegExp.test(input);
}

function convertToUTCDate(inputdate)
{   
	tmpdate= new Date(inputdate.getTime());
	tzoffsetmins= tmpdate.getTimezoneOffset();
	diffr= (tzoffsetmins/60);
	tmpdate.setHours(inputdate.getHours()+diffr);
	return tmpdate;
}
function checkSessionExpired(request){
	var resp=request.responseText;
	if(resp.indexOf("sessionExpired")!=-1){
		if(window.opener){
			window.opener.document.location.href="/";
			window.close();
		}else if(window.top){
			window.top.document.location.href="/";
		}else{
			document.location.href="/";
		}
	}
	if(resp.indexOf("paymentRequired")!=-1){
		if(window.opener){
			window.opener.document.location.href="/subscription/subscriptionInfo";
			window.close();
		}else if(window.top){
			window.top.document.location.href="/subscription/subscriptionInfo";
		}else{
			document.location.href="/subscription/subscriptionInfo";
		}
	}
}
function getLinkPositionArray(linkPostion){
		var positionArray=Array();
		if(linkPostion!=null && linkPostion!='undefined'){
			obj=linkPostion;
			curleft=0;
			curtop =0;
			if(obj && obj.offsetParent) 
			{
				curleft = obj.offsetLeft
				curtop = obj.offsetTop
					var valInc=null;
				while (obj = obj.offsetParent) 
				{
					curleft += obj.offsetLeft
					curtop += obj.offsetTop
						valInc++;
				}			
			}
			positionArray[0]= curtop;
			positionArray[1]= curleft;		
		}
		return 	positionArray;
}