function urlEncode(clearString)
{
	var output='';
	var x=0;
	clearString=clearString.toString();
	var regex=/(^[a-zA-Z0-9_.]*)/;
	while(x<clearString.length){
		var match=regex.exec(clearString.substr(x));
		if(match!=null&&match.length>1&&match[1]!=''){
			output+=match[1];
			x+=match[1].length;
		}else{
			if(clearString[x]==' ')
			output+='+';else{
				var charCode=clearString.charCodeAt(x);
				var hexVal=charCode.toString(16);output+='%'+(hexVal.length<2?'0':'')+hexVal.toUpperCase();
			}
				x++;}
	}
		
	return output;
}


function pausecomp(millis)
{
	var date = new Date();
	var curDate = null;
	do { curDate = new Date(); }
	while(curDate-date < millis);
}


function alphanumericonly(e)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;
// alphas and numbers
else if ((("abcdefghijklmnopqrstuvwxyz0123456789_").indexOf(keychar) > -1))
   return true;
else
   return false;
}


function alphaonly(myfield,e)
{
	var key;
	var keychar;
	 	
	 if (window.event) key = window.event.keyCode;
	 	else if (e) key = e.which;
	 else return true;	 
	 	keychar = String.fromCharCode(key);	 
	 
	 if (iscontrolkey(key)) return true;
	 // numbers or decimal
	 else if ((("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").indexOf(keychar) > -1)) return true;
	 else return false;		
}


function numericonly(myfield,e)
{
	var key;
	var keychar;
	 	
	 if (window.event) key = window.event.keyCode;
	 	else if (e) key = e.which;
	 else return true;	 
	 	keychar = String.fromCharCode(key);	 
	 
	 if (iscontrolkey(key)) return true;
	 // numbers or decimal
	 else if ((("0123456789").indexOf(keychar) > -1)) return true;
	 else return false;		
}

function validateEmail(value)
{	
	var field=value; 		
	
	with (field)
	{
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) 
		  {return false;}
		else{return true;}
	}	
}


function isAlphabetNumeric(strData){
	
	var iCount,iDataLen;
	var strCompare;
	var bFlag = true;
	
	strCompare = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	iDataLen = strData.length;
	
	if (iDataLen >0)
	{
	
	for (iCount=0; iCount<iDataLen;iCount++)
	{
		var cData = strData.charAt(iCount);
		if (strCompare.indexOf(cData) < 0 )
		{			
			return false;
		} 
	}
	return true;
	}
	else{
	return false;
	}
}

function changeFocus(currentId, nextId, maxCount){
	var currentElement = document.getElementById(currentId);
	var nextElement = document.getElementById(nextId);
	
	allowOnlyNumeric(currentId);

	if(currentElement.value.length == maxCount){
		nextElement.focus();
	}
}

function allowOnlyNumeric(id){
	var element = document.getElementById(id);

	if(!isNumeric(element)){
		element.value = element.value.substring(0, element.value.length - 1) 
	}
}

/*   for credit card vaildation */

function isValidCreditCard(type, ccnum) {
	//alert(type);
	//alert(ccnum);
	if (type == "Visa") {
      // Visa: length 16, prefix 4, dashes optional.
      var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "MasterCard") {
      // Mastercard: length 16, prefix 51-55, dashes optional.
      var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "Discover") {
      // Discover: length 16, prefix 6011, dashes optional.
      var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
   } 
   
   if (!re.test(ccnum)) return false;
 
   // Remove all dashes for the checksum checks to eliminate negative numbers
   ccnum = ccnum.split("-").join("");
   // Checksum ("Mod 10")
   // Add even digits in even length strings or odd digits in odd length strings.
   var checksum = 0;
   for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
      checksum += parseInt(ccnum.charAt(i-1));
   }
   // Analyze odd digits in even length strings or even digits in odd length strings.
   for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
      var digit = parseInt(ccnum.charAt(i-1)) * 2;
      if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
   }
   if ((checksum % 10) == 0){ 
	//alert("Valid");
	return true; 
   }else{
	// alert("Not Valid");
	return false;
   }
   
   
}


/* end credit card validation */

function validatePaypalForm()
{
	var x=""; 
	warning=""; 
	var d = new Date();
	var curmonth = d.getMonth()+1;
	
	var curyear = d.getFullYear();
	if(($('#expDateMonth').val()<curmonth && $('#expDateYear').val()<=curyear) || ($('#expDateYear').val()<curyear))
	{
		document.getElementById("expDateMonth").style.border= "1px solid red";
		document.getElementById("expDateYear").style.border= "1px solid red"; 			
		okToGo1 = 'false'; 	
		
		document.getElementById("expiration_date").style.backgroundColor = "#FFC9BB"; 
		document.getElementById("expiration_date").innerHTML = "Please select proper Expiration Date"
		warning+="1";
	}
	else
	{
		
		document.getElementById("expDateMonth").style.border= "1px solid #7F9DB9"; 	
		document.getElementById("expDateYear").style.border= "1px solid #7F9DB9"; 	
		document.getElementById("expiration_date").style.backgroundColor = "#FFFFFF"; 
		document.getElementById("expiration_date").innerHTML = ""
		
	  okToGo1 = 'true';
	}
	if(document.getElementById("firstName").value=="")
	{	document.getElementById("firstName").style.border= "1px solid red"; 			
		okToGo1 = 'false'; 	
		warning+="1";
		
	}else
	{ document.getElementById("firstName").style.border= "1px solid #7F9DB9"; 	
	  okToGo1 = 'true';
	  
	} 
	if(document.getElementById("lastName").value=="")
	{	
		document.getElementById("lastName").style.border= "1px solid red"; 			
		okToGo1 = 'false'; 	
		warning+="1";
		
	}else
	{ document.getElementById("lastName").style.border= "1px solid #7F9DB9"; 	
	  okToGo1 = 'true';
	 
	}
	
	if(document.getElementById("creditCardNumber").value=="")
	{	
		document.getElementById("creditCardNumber").style.border= "1px solid red"; 			
		okToGo1 = 'false'; 	
		warning+="1";
		
	}else if(!isValidCreditCard(document.getElementById("creditCardType").value,document.getElementById("creditCardNumber").value)){
		document.getElementById("creditCardNumber").style.border= "1px solid red"; 			
		okToGo1 = 'false'; 	
		document.getElementById("creditCardNumberid").style.backgroundColor = "#FFC9BB"; 
		document.getElementById("creditCardNumberid").innerHTML = "Please enter valid card no";
		warning+="1";
	
	}else
	{ document.getElementById("creditCardNumber").style.border= "1px solid #7F9DB9";
	  document.getElementById("creditCardNumberid").style.backgroundColor = ""; 
	  document.getElementById("creditCardNumberid").innerHTML = "";	
	  okToGo1 = 'true';
	 
	}
	
	if(document.getElementById("cvv2Number").value=="")
	{	
		document.getElementById("cvv2Number").style.border= "1px solid red"; 			
		okToGo1 = 'false'; 	
		warning+="1";
		
	}else
	{ document.getElementById("cvv2Number").style.border= "1px solid #7F9DB9"; 	
	  okToGo1 = 'true';
	 
	}
	
	
	if(document.getElementById("address1").value=="")
	{	
		document.getElementById("address1").style.border= "1px solid red"; 			
		okToGo1 = 'false'; 	
		warning+="1";
		
	}else
	{ document.getElementById("address1").style.border= "1px solid #7F9DB9"; 	
	  okToGo1 = 'true';
	 
	}
	
	if(document.getElementById("city").value=="")
	{	
		document.getElementById("city").style.border= "1px solid red"; 			
		okToGo1 = 'false'; 	
		warning+="1";
		
	}else
	{ document.getElementById("city").style.border= "1px solid #7F9DB9"; 	
	  okToGo1 = 'true';
	 
	}
	
	if(document.getElementById("state").value=="")
	{	
		document.getElementById("state").style.border= "1px solid red"; 			
		okToGo1 = 'false'; 	
		warning+="1";
		
	}else
	{ document.getElementById("state").style.border= "1px solid #7F9DB9"; 	
	  okToGo1 = 'true';
	 
	}
	
	if(document.getElementById("country").value=="")
	{	
		
		document.getElementById("country").style.border= "1px solid red"; 			
		okToGo1 = 'false'; 	
		document.getElementById("countryid").style.backgroundColor = "#FFC9BB"; 
		document.getElementById("countryid").innerHTML = "Please select country";
		warning+="1";
		
	}else
	{
		
		 document.getElementById("country").style.border= "1px solid #7F9DB9"; 	
		 document.getElementById("countryid").style.backgroundColor = "#FFFFFF"; 
		document.getElementById("countryid").innerHTML = "";
	  okToGo1 = 'true';
	 
	}
	if(warning=="") 
	return true;
	else
	return false;
}


function convert_24to12Format(timeIn24)
{
	var time = timeIn24.split(":");
	
	if(time[0].substr(0,1)=="0")
	{
	   time[0] = time[0].substr(1,1); 
	}
  	var hour = parseInt(time[0]); 
  	
	var minute = time[1]; 
	
	if(hour < 12)
	{//it's am print the hour as is and add am		
	    if(hour <10)
		{
		 var hourDisplay  = "0"+hour.toString(); 
		}else
		{
			var hourDisplay  = hour.toString(); 
		}
	    
		return hourDisplay + ":" + minute + "am";	
	}
	else if(hour >= 12 && hour < 24) //subtract 12 and add pm 
	{
		if (hour>12)
		{
			hour = hour-12; 
		}
		
		return hour.toString()+":"+minute+"pm"; 	
	}
}


