/*
 * Converts the first character into upper case
 */

function makeFirstCapital(text)
{
	text.value=text.value.substring(0, 1).toUpperCase()+text.value.substring(1, text.value.length);
	return true;
}

/*
 * Checks the field is empty or not 
 */
 
 function LTrim( value ) {
   	
    	var re = /\s*((\S+\s*)*)/;
     	return value.replace(re, "$1");
     	
     }
     
   // Removes ending whitespaces
   function RTrim( value ) {
    	
    	var re = /((\s*\S+)*)\s*/;
    	return value.replace(re, "$1");
    	
    }
    
    // Removes leading and ending whitespaces
   function trim( value ) {
    	
    	return LTrim(RTrim(value));
    	
    }
	
function isBlank(text,message)
{
	with(text)
	{
		trim_value=trim(value);
		if (trim_value==null||trim_value=="")
		{
			document.getElementById("javscripterrors").innerHTML = "Please enter "+message+".";	
			

			text.focus();
			
			return false;
		}	
		return true;
	}
}
function isValidate(text,message)
{
	with(text)
	{
		trim_value=trim(value);
		
		if (trim_value==null||trim_value=="")
		{
			document.getElementById("javscripterrors").innerHTML = "Please enter "+message+".";	
			text.style.borderColor="red";		
			
			return false;
		}	
		   
		
		return true;
	}
}




function isSpclChars(text,message) {

 var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";

for (var i = 0; i < text.value.length; i++) {
  	if (iChars.indexOf(text.value.charAt(i)) != -1) {
  	document.getElementById("javscripterrors").innerHTML = "Please enter valid "+message+".";	
			text.style.borderColor="red";

			text.focus();
  	return false;
  	}
  }
  return true;
  
}

function isNumeric(text,message) {

var nums = /^[0-9]*$/;
if (!nums.test(text.value)) {
    document.getElementById("javscripterrors").innerHTML = "Please enter valid "+message+".";	
			text.style.borderColor="red";

			text.focus();
     return false;
}
return true;
 
}

function isBlankHomePage2(text,message)
{
	with(text)
	{
		trim_value=trim(value);
		if (trim_value==null||trim_value=="")
		{
			document.getElementById("javscripterrors2").innerHTML = message;			
			text.focus();
			return false;
		}	
		return true;
	}
}
function isBlankHomePage(text,message)
{
	with(text)
	{
		trim_value=trim(value);
		if (trim_value==null||trim_value=="")
		{
			document.getElementById("javscripterrors").innerHTML = message;			
			text.focus();
			return false;
		}	
		return true;
	}
}
function contatinsSpclChars(text,message)
{


/*	if (!(/[^a-z0-9 ]/i.test(text.value))) {
	alert("Invalid Characters");
		//document.getElementById("javscripterrors").innerHTML = message+" should not contain special characters and spaces.";
				
		text.focus();
		return false;
	}*/
	 
	return true;
}
function checkNumreic(text,message){
if (!(/^[A-Z\a-z\d\@\.\_\-]*$/i.test(text))) {
	
	document.getElementById("javscripterrors").innerHTML = " Please enter valid "+message+".";	
		text.focus();	
	return false
	}
	}

function checkSelect(text,message)
{	
	if(text.value=="")
	{
		document.getElementById("javscripterrors").innerHTML = "Please select "+message+".";		
		text.focus();
		return false;
	}
	return true;	
}

function checkLength(text,message,low,high)
 {
	 if(text.value.length<low||text.value.length>high)
	 {
	 	document.getElementById("javscripterrors").innerHTML = message+" length must be greater than "+low+" and less than "+high+" characters.";
		text.focus();
		return false;
	 }
	 return true;	 
}



function isContact(text,message)
 {
 	 if(isNaN(text.value))
 	 {
 	 	document.getElementById("javscripterrors").innerHTML = message+" number should contain digits only.";
		text.focus();
		return false;
 	 }
	 if(text.value.length!=10)
	 {
		document.getElementById("javscripterrors").innerHTML = message+" number should be 10 digits only.";
		text.focus();
		return false;
	 }
	 return true;	 
 }
 
 function isEmailCheck(text,message) 
 {
		var str=text.value;
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		var lldot = str.lastIndexOf(dot);
		if (str==""){
		   return true;
		}

		if (str.indexOf(at)==-1){
		   document.getElementById("javscripterrors").innerHTML = "Please enter a valid "+message;
		   text.style.borderColor="red";
		  text.focus();
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		  document.getElementById("javscripterrors").innerHTML = "Please enter a valid "+message;
		  text.style.borderColor="red";
		   //text.focus();
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   document.getElementById("javscripterrors").innerHTML = "Please enter a valid "+message;
		   text.style.borderColor="red";
		 //  text.focus();
		   return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		     document.getElementById("javscripterrors").innerHTML = "Please enter a valid "+message;
		     text.style.borderColor="red";
		   //text.focus();
		   return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		     document.getElementById("javscripterrors").innerHTML = "Please enter a valid "+message;
		     text.style.borderColor="red";
		   //text.focus();
		   return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		      document.getElementById("javscripterrors").innerHTML = "Please enter a valid "+message;
		      text.style.borderColor="red";
		   //text.focus();
		   return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		      document.getElementById("javscripterrors").innerHTML = "Please enter a valid "+message;
		      text.style.borderColor="red";
		   //text.focus();
		   return false;
		 }
		 
		 if(lldot == (lstr-1) )
		 {
		 	document.getElementById("javscripterrors").innerHTML = "Please enter a valid "+message;
		 	text.style.borderColor="red";
		   //text.focus();
		   return false;
		 }
		 document.getElementById("javscripterrors").innerHTML = "";

 		 return true;					
	}

function isEmailCheckProdReg(text) 
 {
		var str=text.value;
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		var lldot = str.lastIndexOf(dot);
		if (str==""){
		   return true;
		}
		if (str.indexOf(at)==-1){
		   text.focus();
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   text.focus();
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   text.focus();
		   return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   text.focus();
		   return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   text.focus();
		   return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		   text.focus();
		   return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		   text.focus();
		   return false;
		 }
		 
		 if(lldot == (lstr-1) )
		 {
		   text.focus();
		   return false;
		 }
		
 		 return true;					
	}
	
function isSame(text1,text2)
{
if(text1.value != "" && text2.value != ""){
	if(text1.value == text2.value)
	{
		return true;		
	}
	else
	{
		document.getElementById("javscripterrors").innerHTML = "Confirm Email Address does not match";
		text2.style.borderColor="red";
		return false;
	}
	}
}
function validZip(zip)
{
	zip=zip.toUpperCase();
	
	if (zip.match(/^[A-Z][0-9][A-Z] [0-9][A-Z][0-9]$/)) {
	return true;
	}
	return false;	
}
function validZipUS(zip)
{
	zip=zip.toUpperCase();
	
	if (zip.match(/^\d{5}$/)) {
	return true;
	}
	
	return false;
	
}


var formValid;
function check(url,form)
{
	formValid = form;
	url += "?model="+form.modelnum.value;
			if (window.XMLHttpRequest) 
			{ 
				// Non-IE browsers
					req = new XMLHttpRequest();       
					req.onreadystatechange = processStateChange;
					try 
					{
						 req.open("GET", url, true);
					}
					catch (e) 
					{
						 alert(e);
					}
					req.send(null);
			} 
			else if (window.ActiveXObject) 
			{ // IE
			
				 req = new ActiveXObject("Microsoft.XMLHTTP");
				if (req) 
				{
					 req.onreadystatechange = processStateChange;
					 req.open("GET", url, true);
					 req.send();
				}
			}
    
//		alert("ajax code worked properly");
}

function processStateChange() 
{
	if (req.readyState == 4) 
	{ // Complete
		if (req.status == 200) 
		{ // OK response	
				
			if(trim(req.responseText) != "valid")
			{			
				//You have entered "xxxx " as the model number.  Please re-confirm.  Thank you.
				if(confirm("You have entered '"+req.responseText+"' as the model number. Please verify. \nPress OK to Continue / Cancel to Re-enter." ))
				formValid.submit();
				
				
			}
			else{
				formValid.submit();
			}
		} 			
		else
		{
			 alert("Problem: " + req.statusText);
		}
	}
}
	  
function date_check(field1,field2,message)
{
	
	
	var effective_array=field1.value.split("/");
	var eff_mm=parseInt(effective_array[0]);
	var eff_dd=parseInt(effective_array[1]);
	var eff_yy=parseInt(effective_array[2]);
	
	var expiry_array=field2.value.split("/");
	var ex_mm=parseInt(expiry_array[0]);
	var ex_dd=parseInt(expiry_array[1]);
	var ex_yy=parseInt(expiry_array[2]);
	if(ex_yy<eff_yy)
	{
	document.getElementById("javscripterrors").innerHTML = message;
	field1.focus();
	return false;
	}
	else if(ex_yy==eff_yy)
	{
		if(ex_mm<eff_mm)
		{
		document.getElementById("javscripterrors").innerHTML = message;
		   field1.focus();
		   return false;
		}
		else if(ex_mm==eff_mm)
		{
			if(ex_dd<eff_dd)
			{
			document.getElementById("javscripterrors").innerHTML = message;
		   field1.focus();
		   return false;
			}
			
		}
		
	}
	return true;

}

function validDate(field,fielddate){
	with(field)
	{
	  var endDate = new Date(value); 
	  var currentdate=new Date();
	  
	  if(endDate>currentdate)
	  {
	  alert( "Birth Date should not be greater than the current date") ;
	  field.focus();
	  return false;
	  }
	  else 
	  return true;
	}
}
function showthemes()
{
	document.getElementById("homelink").style.display = "none";
	document.getElementById("logintablink").style.visibility = "visible";
	document.getElementById("themes").style.visibility = "visible";	
}
function checkSize(size,field)
{
	var data = field.value;
	if(data.length >=size)
	data = data.substring(0,size);
	field.value = data;
}

function voteStar(wishid,field,imagefield)
{
	var url = "blesswishaction?wishid="+wishid+"&type=star";	
	setup(url,field,imagefield);
}
function voteThumb(wishid,field,imagefield)
{
	var url = "blesswishaction?wishid="+wishid+"&type=thumbs";	
	setup(url,field,imagefield);
}
function voteRoses(wishid,field,imagefield)
{
	var url = "blesswishaction?wishid="+wishid+"&type=roses";	
	setup(url,field,imagefield);
}

function song(field,srcfile)
{
	//alert(srcfile);
	if(trim(document.getElementById(field).innerHTML) != "Click play to listen this Wish.")
	{
		document.getElementById(field).innerHTML="Click play to listen this Wish.";	
		document.getElementById("play"+field).style.backgroundImage = "url(../images/secure/play.png)";
	}
	else
	{
		document.getElementById(field).innerHTML="<img src='../images/secure/loader.gif'/><object type='application/x-mplayer2' id='music1' pluginspage='../application/wmpfirefoxplugin.exe' src='"+srcfile+"' width='0' height='0' controltype='false' showcontrols='0' showstatusbar='0' AutoStart='true' align='baseline' ></object>";
	//	document.getElementById(field).innerHTML="<img src='../images/loader.gif'/><OBJECT ID='MediaPlayer' WIDTH='0' HEIGHT='0' CLASSID='clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA' TYPE='application/x-oleobject' pluginspage='../application/wmpfirefoxplugin.exe'><EMBED TYPE='application/x-mplayer2' SRC='"+srcfile+"' NAME='MediaPlayer' ShowStatusBar='0' ShowDisplay='0' autostart='1'> </EMBED></OBJECT>";
		document.getElementById("play"+field).style.backgroundImage = "url(../images/secure/stop.png)";
	}
}

function isValidate_product(){

  var firstName= document.getElementById("fname");
  var lastName = document.getElementById("lname");
  var address = document.getElementById("address");
  var city = document.getElementById("city");
  var state = document.getElementById("state");
  var country = document.getElementById("country");
  var zip=document.getElementById("zip");
  var phoneNumber=document.getElementById("phonenumber");
  var email = document.getElementById("email");
  var emailcnf = document.getElementById("emailcnf");
  var purchasedate = document.getElementById("purchasedate");
  var modelNumber = document.getElementById("modelnum");
  var storeName1 = document.getElementById("purchasedfrom");
  
  var msg_firstName="First Name";
  var msg_lastName="Last Name";
  var msg_address="Address";
  var msg_city="City";
  var msg_state="State/Province";
  var msg_country="Country";
  var msg_zip="Zip";
  var msg_phoneNumber="Phone Number";
  var msg_email="Email Address";
  var msg_emailcnf="Confirm Email Address";
  var msg_purchasedate="Purchase Date";
  var msg_modelNumber="Model Number";
  var msg_storeName1="Where did you purchase your product";
  
  var valid_email="Email Address";
  var valid_emailcnf = "Confirm Email Address";
  
  var fields=[firstName,lastName,address,city,state,country,zip,phoneNumber,email,emailcnf,purchasedate,modelNumber,storeName1]; 
  var message=[msg_firstName,msg_lastName,msg_address,msg_city,msg_state,msg_country,msg_zip,msg_phoneNumber,msg_email,msg_emailcnf,msg_purchasedate,msg_modelNumber,msg_storeName1];
  var valid_message=[valid_email,valid_emailcnf];
  
  var i=0;
  for( i=0;i < fields.length; i++) {
  if(trim(fields[i].value) == "") {
    document.getElementById("javscripterrors").innerHTML = "Please enter "+message[i]+".";
    fields[i].style.borderColor="red";
    fields[i].focus();
    return false;
    }
     if(fields[i] == email) {
      
      if(!isEmailCheck(fields[i],valid_message[0]))
      return false;
      }
    
      if(fields[i] == emailcnf){
 
       if(!isEmailCheck(fields[i],valid_message[1]))
       return false;
 
       if(!isSame(email,emailcnf)){
        emailcnf.focus();
        return false;
        }
       }
  } 
  return true;
}

function isValidate_optin(){

  var firstName= document.getElementById("txtFirstName");
  var lastName = document.getElementById("txtLastName");
  var email = document.getElementById("txtEmail");
  var emailcnf = document.getElementById("txtConfirmEmail");
 
  var msg_firstName="First Name";
  var msg_lastName="Last Name"; 
  var msg_email="Email Address";
  var msg_emailcnf="Confirm Email Address";
  
  var valid_email="Email Address";
  var valid_emailcnf = "Confirm Email Address";
  
  
  
  var fields=[firstName,lastName,email,emailcnf];
  var message=[msg_firstName,msg_lastName,msg_email,msg_emailcnf];
  var valid_message=[valid_email,valid_emailcnf];
  
  for(var i=0;i < fields.length; i++){
  if(trim(fields[i].value) == ""){
    document.getElementById("javscripterrors").innerHTML = "Please enter "+message[i]+".";
    fields[i].style.borderColor="red";
    fields[i].focus();
    return false;
    }
  
  if(fields[i] == email){
 
   if(!isEmailCheck(fields[i],valid_message[0]))
   return false;
   }
  
  if(fields[i] == emailcnf){
 
   if(!isEmailCheck(fields[i],valid_message[1]))
   return false;
 
   if(!isSame(email,emailcnf)){
    emailcnf.focus();
    return false;
    }
   } 
 }
 return true;  
}

function validateTxt(text)
	{
	if (trim(text.value) != "")
		{		
			document.getElementById("javscripterrors").innerHTML = "<br>";
			
			text.style.borderColor="";			
			
		}	  
		
	
	}
