/*
Strip whitespace from the beginning and end of a string
Input : a string
UPDATE:
		jpw 8/7/2010 - corrected the copyinfo function to properly copy selected state and country
		jpw 8/17/2010 - fixed the email and phone verification. Line 345 and following
		jpw 8/18/2010 - cleared the user_name, o_shop and email that is already taken.
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}


					




function hasSpecialChar(str)
{
  var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
  
  for (var i = 0; i < str.length; i++) {
  	if (iChars.indexOf(str.charAt(i)) != -1) {
  	return true;
  	}
  }
  
  return false;
}

function isallword(str)
{
  var iChars = "!@$%^&*()+=-[]\\\';,/{}|\":<>?";
  
  for (var i = 0; i < str.length; i++) {
  	if (iChars.indexOf(str.charAt(i)) != -1) {
  	return true;
  	}
  }
  
  return false;
}

function check_user()
{
	document.getElementById("user_check").innerHTML = " <img src='/images/preloader-check.gif' width='15px' />Checking Availability...";


	if(document.getElementById("user_name").value == "" ){
		document.getElementById("user_check").innerHTML = " <img src='/images/wrong.gif' width='15px' /> Blank User Name";
		document.form1.user_name.value = "";
		return false;
	}

	if(hasSpecialChar(document.getElementById("user_name").value)){
		document.getElementById("user_check").innerHTML = " <img src='/images/wrong.gif' width='15px' /> \"!@#$%^&*()+=-[]\\\';,./{}|\":<>?\" Characters are Invalid";
		//document.form1.user_name.value = "";
		return false;
	}
	
	$.ajax({
   		type: "POST",
   		url: "check_user.php",
		async: false,
   		data: "action=user&value=" + document.getElementById("user_name").value,
   		success: function(msg){
			if(msg == "true") {
					document.getElementById("user_check").innerHTML = " <img src='/images/wrong.gif' width='15px' /> '" + document.getElementById("user_name").value + "' already taken.";
				} else {
					document.getElementById("user_check").innerHTML = " <img src='/images/check.gif' width='15px' /> '" + document.getElementById("user_name").value + "' is Available.";
				}
   		}
 	});
}


function check_shop()
{
	document.getElementById("shop_check").innerHTML = " <img src='/images/preloader-check.gif' width='15px' />Checking Availability...";

/*	if(hasSpecialChar(document.getElementById("O_store").value)){
		document.getElementById("shop_check").innerHTML = " <img src='/images/wrong.gif' width='15px' /> \"!@#$%^&*()+=-[]\\\';,./{}|\":<>?\" Characters are Invalid";		
	}*/

	if(document.getElementById("O_store").value == ""){
		document.getElementById("shop_check").innerHTML = " <img src='/images/wrong.gif' width='15px' /> Blank Store Name";
		return;
	}

	$.ajax({
   		type: "POST",
   		url: "check_user.php",
		async: false,
   		data: "action=subdomain&value=" + document.getElementById("O_store").value,
   		success: function(msg){
			if(msg == "true") {
					document.getElementById("shop_check").innerHTML = " <img src='/images/wrong.gif' width='15px' /> '" + document.getElementById("O_store").value + "' already taken.";
				} else {
					document.getElementById("shop_check").innerHTML = " <img src='/images/check.gif' width='15px' /> '" + document.getElementById("O_store").value + "' is Available.";
				}
   		}
 	});
}

function check_email()
{
	if ( document.getElementById("O_email").value.search(/@/) < 2) {
		document.getElementById("email_check").innerHTML = " <img src='/images/wrong.gif' width='15px' />Email is Invalid.";					
	}
	document.getElementById("email_check").innerHTML = " <img src='/images/preloader-check.gif' width='15px' />Checking Availability...";
	document.getElementById("email_field").value = "";

/*	if(hasSpecialChar(document.getElementById("O_store").value)){
		document.getElementById("shop_check").innerHTML = " <img src='/images/wrong.gif' width='15px' /> \"!@#$%^&*()+=-[]\\\';,./{}|\":<>?\" Characters are Invalid";
		return;
	}*/

	$.ajax({
   		type: "POST",
   		url: "check_user.php",
		async: false,
   		data: "action=email&value=" + document.getElementById("O_email").value,
   		success: function(msg){
			if(msg == "true") {
					document.getElementById("email_check").innerHTML = " <img src='/images/wrong.gif' width='15px' />Email already taken.";
				} else {
					document.getElementById("email_check").innerHTML = " <img src='/images/check.gif' width='15px' /> Email is Available.";
					document.getElementById("email_field").value = "true";
				}
   		}
 	});
}

function offsite_check_user()
{
	document.getElementById("user_check").innerHTML = " <img src='/images/preloader-check.gif' width='15px' />Checking Availability...";
	
	if(hasSpecialChar(document.getElementById("user_name").value)){
		document.getElementById("user_check").innerHTML = " <img src='/images/wrong.gif' width='15px' /> \"!@#$%^&*()+=-[]\\\';,./{}|\":<>?\" Characters are Invalid";
	}
	
	$.ajax({
   		type: "POST",
   		url: "offsite_check_user.php",
		async: false,
   		data: "action=user&value=" + document.getElementById("user_name").value,
   		success: function(msg){
			if(msg == "true") {
					document.getElementById("user_check").innerHTML = " <img src='/images/wrong.gif' width='15px' /> '" + document.getElementById("user_name").value + "' already taken.";
				} else {
					document.getElementById("user_check").innerHTML = " <img src='/images/check.gif' width='15px' /> '" + document.getElementById("user_name").value + "' is Available.";
				}
   		}
 	});
}


function offsite_check_shop()
{
	document.getElementById("shop_check").innerHTML = " <img src='/images/preloader-check.gif' width='15px' />Checking Availability...";

	if(hasSpecialChar(document.getElementById("O_store").value)){
		document.getElementById("shop_check").innerHTML = " <img src='/images/wrong.gif' width='15px' /> \"!@#$%^&*()+=-[]\\\';,./{}|\":<>?\" Characters are Invalid";
		document.getElementById("shop_field").value = "";
		return false;
	}

	$.ajax({
   		type: "POST",
   		url: "offsite_check_user.php",
		async: false,
   		data: "action=shop&value=" + document.getElementById("O_store").value,
   		success: function(msg){
			if(msg == "true") {
					document.getElementById("shop_check").innerHTML = " <img src='/images/wrong.gif' width='15px' /> '" + document.getElementById("O_store").value + "' already taken.";
					document.getElementById("shop_field").value = "";
				} else {
					document.getElementById("shop_check").innerHTML = " <img src='/images/check.gif' width='15px' /> '" + document.getElementById("O_store").value + "' is Available.";
					document.getElementById("shop_field").value = "true";
				}
   		}
 	});
}

function  putName(){
	
	with (window.document.form1) {
		O_name.value = O_fname.value + " " + O_mname.value + " " + O_lname.value;
	}
	
}

function  copyinfo(){

	with (window.document.form1) {
		if (C_copy.checked){ 
			C_fname.value = O_fname.value;
			C_mi.value = O_mname.value;
			C_lname.value = O_lname.value;
			C_address.value = O_street.value;
			C_billing_apt.value = O_prt.value;
			C_city.value = O_city.value;

			C_zip.value = O_zip.value;

			// jpw correcte the copy of information for the country
			C_country.selectedIndex = O_country.selectedIndex;
			//C_country.selectedIndex.value = O_country.selectedIndex.value;
			//C_country.selectedIndex.text = O_country.selectedIndex.text;

			C_telephone.value = O_phone.value;
			C_email.value = O_email.value;

			// jpw corrected the copy of information for the state
			//C_state.value = O_state.value;
			C_state.selectedIndex = O_state.selectedIndex;
			//C_state.selectedindex.value = O_state.selectedIndex.value;
			//C_state.selectedindex.text = O_state.selectedIndex;

		}else{
			C_fname.value = "";
			C_mi.value = "";
			C_lname.value = "";
			C_address.value= "";
			C_billing_apt.value = "";
			C_city.value = "";
			// jpw corrected the clearing of information for the state
			C_state.selectedindex = "";
			//C_state.selectedIndex.value = "";
			C_state.selectedIndex.text = "";
			C_zip.value = "";
			// jpw corrected the clearing of information for the country
			//C_country.value = "";
			C_country.selectedIndex = "";
			C_country.selectedIndex.value = "";
			C_country.selectedIndex.text = "";

			C_telephone.value = "";
			C_email.value = "";
		}
	}
	
}

function checkShippingAndPaymentInfo()
{
	with (window.document.form1) {
		if (isEmpty(user_name, 'User Name')) {
			return false;
		} else if (isEmpty(password, 'Password is required')) {
			return false;
		} else if (isEmpty(password2, 'Passwords must match')) {
			return false;
		} else if (password.value != password2.value) {
			alert("Password and Re-type Password don't match");
			return false;	
		} else if (isEmpty(O_store, 'Store Name')) {
			return false;
		} else if (isEmpty(O_fname, 'Owner\'s First Name')) {
			return false;
		} else if (isEmpty(O_lname, 'Owner\'s Last Name')) {
			return false;
		} else if (isEmpty(O_street, 'Owner\'s Street Address')) {
			return false;
		}/* else if (isallword(O_street.value)) {
			alert("Invalid Street. Please don't include special character");
			return false;
		}*/ else if (isEmpty(O_city, 'Owner\'s City')) {
			return false;
		} else if (isallword(O_city.value)) {
			alert("Invalid City. Please don't include special character");
			return false;
		} else if (isEmpty(O_state, 'Owner\'s State')) {
			return false;
		} else if (isEmpty(O_zip, 'Owner\'s Zip/Postal Code')) {
			return false;
		} else if (O_zip.value.length < 5 && parseInt(O_zip.value) > 999999999) {
			alert("Invalid Zip Code");
			return false;		
		} else if (isEmpty(O_phone, 'Owner\'s Phone Number')) {
			return false;
		} else if (isEmpty(O_country, 'Owner\'s Country')) {
			return false;
		} else if (isEmpty(O_email, 'Owner\'s Email')) {
			return false;
		} else if ( O_email.value.search(/@/) < 2) {
			alert("The owner email address entered is not valid.  Please enter a valid email address.");
			return false;
		} /* else if ( email_field.value != "true") {
			alert("The owner email address entered is already taken.");
			return false;
		}*/else if (isEmpty(C_fname, 'Card Holder\'s First Name')) {
			return false;
		} else if (isEmpty(C_lname, 'Card Holder\'s Last Name')) {
			return false;
		} else if (isEmpty(C_address, 'Card Holder\'s Address')) {
			return false;
		} else if (isEmpty(C_city, 'Card Holder\'s City')) {
			return false;
		} else if (isEmpty(C_state, 'Card Holder\'s State')) {
			return false;
		} else if (isEmpty(C_zip, 'Card Holder\'s Zip/Postal Code')) {
			return false;
		} else if (isEmpty(C_country, 'Card Holder\'s Country')) {
			return false;
		} else if (isEmpty(C_telephone, 'Card Holder\'s Telephone Number')) {
			return false;
		} else if (isEmpty(C_email, 'Card Holder\'s Email')) {
			return false;
		} else if (isEmpty(user_name, 'Invalid User Name.')) {
			return false;
		}else if (isEmpty(O_store, 'Invalid Shop Name.')) {
			return false;
		}/*else if (isallword(C_address.value)) {
			alert("Invalid Street. Please don't include special character");
			return false;
		}*/else if (isallword(C_city.value)) {
			alert("Invalid City. Please don't include special character");
			return false;
		}else if (C_zip.value.length < 5 && parseInt(C_zip.value) > 999999999) {
			alert("Invalid Zip Code");
			return false;
		} else if ( C_email.value.search(/@/) < 2) {
			alert("The Card Holder email address entered is not valid.  Please enter a valid email address.");
			return false;
		} else {
			return true;
		}
	}
}
	
	function checkNewAssociationtInfo()
{
	with (window.document.frmOrganization) 
	 {
		if (isEmpty(txtOrgName, 'Organization Name')) 
		{
			return false;
		} else if (isEmpty(txtName, 'Your Name'))
		{
			return false;
		} 
		//=========================================================================
		//Lets validate Email
		else if ((txtEmail.value.length!=0) &&  (txtEmail.value!=null))
		{
		   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		   var address = txtEmail.value;
		  	 if(reg.test(address) == false) 
			 	{
				  alert('Invalid Email Address');
	    		  return false;		
	  			}
			  else	
			  	{
		 		  return true;					
				}
	
		} 
//==========================================================================		
		else 
		{
			return true;
		}
	}
}
	
	



//==========================================================================		
<!-- Phone Number Keypress
<!-- Begin
var n;
var p;
var p1;
function ValidatePhone(){
p=p1.value
if(p.length==3){
	//d10=p.indexOf('(')
	pp=p;
	d4=p.indexOf('')
	d5=p.indexOf('-')
	if(d4==-1){
		pp=""+pp;
	}
	if(d5==-1){
		pp=pp+"-";
	}
	//pp="("+pp+")";
	//document.form1.O_phone.value="";
	//document.form1.O_phone.value=pp;
document.getElementById(n).value="";
			document.getElementById(n).value=pp;
}
if(p.length>3){
	d1=p.indexOf(' ')
	d2=p.indexOf('-')
	if (d2==-1){
		l30=p.length;
		p30=p.substring(0,4);
		//alert(p30);
		p30=p30+"-"
		p31=p.substring(4,l30);
		pp=p30+p31;
		//alert(p31);
	//	document.form1.O_phone.value="";
//		document.form1.O_phone.value=pp;
			document.getElementById(n).value="";
			document.getElementById(n).value=pp;
	}
	}
if(p.length>5){
	p11=p.substring(d1+1,d2);
	if(p11.length>3){
	p12=p11;
	l12=p12.length;
	l15=p.length
	//l12=l12-3
	p13=p11.substring(0,3);
	p14=p11.substring(3,l12);
	p15=p.substring(d2+1,l15);
	//document.form1.O_phone.value="";
	pp=""+p13+"-"+p14+p15;
	//document.form1.O_phone.value=pp;
document.getElementById(n).value="";
			document.getElementById(n).value=pp;
	}
	l16=p.length;
	p16=p.substring(d2+1,l16);
	l17=p16.length;
	if(l17>3&&p16.indexOf('-')==-1){
		p17=p.substring(d2+1,d2+4);
		p18=p.substring(d2+4,l16);
		p19=p.substring(0,d2+1);
		//alert(p19);
	pp=p19+p17+"-"+p18;
	//document.form1.O_phone.value="";
	//document.form1.O_phone.value=pp;
	p1.value="";
	p1.value=pp;
	}
}
//}
setTimeout(ValidatePhone,100)
}
function getIt(m){
n=m.name;
//alert(n);
//alert(document.getElementById(n).value);
//alert(document.forms[0].elements[n]);
p1=m
//alert(m);
ValidatePhone()
}

/*function testphone(obj1){
p=obj1.value
//alert(p)
p=p.replace("","")
p=p.replace("-","")
p=p.replace("-","")
p=p.replace("-","")
//alert(isNaN(p))
if (isNaN(p)==true){
alert("Check phone");
return false;
}
}*/
//  End -->
//==========================================================================		

//Allow numbers only to be input in Keypress Event
function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
      }

function CheckURLValidation(strUrl1) {
var RegexUrl = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
return RegexUrl.test(strUrl1);
}
	
function PassUrl(strUrl)
{
	RetUrl=CheckURLValidation(strUrl);
	//alert('1st check: '+RetUrl);
		if (RetUrl==false) {
			strUrl =  'http://' + strUrl;
			//alert('2nd concat: '+strUrl);			
			RetUrl=CheckURLValidation(strUrl);
			if (RetUrl==false)	{
				//alert('Invalid Store URL');
				document.getElementById('store_url').value ="";
			}
			else{
				document.getElementById('store_url').value =strUrl;			
			}
		}
		/*else{
		 return RegexUrl.test(strUrl);	
		}*/
}
 

	

