//Prevent conflict bwtween jquery and prototype instead of $ jquery will now use $j
var $j = jQuery.noConflict();
var counter=200;
function restrict_chars(obj)
{
	var val=obj.value;

	try
	{
	if(val.length>200)
	{
		alert("You have reached maximum characters limit.");
		obj.value=obj.value.substring(0,200);
		return false;
	}
	counter=200-(val.length);
	var str=counter+" Characters Remaining." 
	}
	catch(e)
	{
		alert(e);
	}
	document.getElementById('limit_chars').innerHTML=str; 
}
function checkemail_reg(contact_email)
{
	
	
	var str=contact_email;
	var regis=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;

	if (regis.test(str)) { 	return true;
	} else {	
	 	return false;	}
		}
		
function checkphone_reg(contact_phone)
{

	var str1=contact_phone;
	//var regist=/^([\w-]+(?:\.[\w-]+)*)-((?:[\w-]+\.)*\w[\w-]{0,66})\-([0-9](?:\.[0-9])?)$/;
	//var regist=/^([0-9][0-9][0-9]\D?[0-9][0-9][0-9]\D?[0-9][0-9][0-9][0-9])$/;
	var regist=/^[\d- ]+$/;
	//str.match(regexp)
	if(contact_phone.match(regist)) 
	{ 
		return true;
	} 
	else 
	{	
		return false;	
	}
}

function check_contform()
{
	var obj=document.form1;
	var msg="Sorry, we cannot complete your request.\nKindly provide us the missing or incorrect information enclosed below.\n\n";
 	var error=false;
	var x,y,z;
	//if(obj.contact_name.value=='') 
	//{ 
	//msg+="- Please enter your name.\n";  error=true; 
	//x=1;
	//}

	// if neither a valid email nor a valid phone number was entered:
	if(!checkphone_reg(obj.contact_phone.value) && !checkemail_reg(obj.contact_email.value) ) 
	{ 
		msg+="- Please enter either a valid phone number or a valid email address.\n"; 
		y=1;
		//obj.contact_phone.focus();
		error=true; 
	} 	
	// if the user wants to subscribe, he should enter a valid address
	//if(!error && obj.contact_subscribe.value!='' && !checkemail_reg(obj.contact_email.value)) {
	//	msg+="- Please enter either a valid email address to subscribe to our e-zine.\n"; 
	//	z=1;
	//	//obj.contact_email.focus();
	//	error=true; 
	//} 
	// if the user entered an email, it should be a valid one
	// the default string contains "Email" so if the user left the default, it's ok
	if(!error && obj.contact_email.value!='' && contact_email.value.indexof("Email")!=-1 && !checkemail_reg(obj.contact_email.value)) {
		msg+="- Please enter a valid email address.\n"; 
		z=1;
		//obj.contact_phone.focus();
		error=true; 
	} 

	if(!error && obj.contact_phone.value!='' && contact_phone.value.indexof("Phone")!=-1 && !checkphone_reg(obj.contact_phone.value)) {
		msg+="- Please enter a valid phone number.\n"; 
		y=1;
		//obj.contact_phone.focus();
		error=true; 
	} 

	if(error==true) 
	{
		alert(msg); 

		if(x==1)
		{
			obj.contact_name.focus();
		}		
		else if(y==1)
		{
			obj.contact_phone.focus();
		}
		else if(z==1)
		{
			obj.contact_email.focus();
		}
		return false;
	} 
	else 
	{
		return true;
	}
	 
}

function check_subscribeform()
{
	var obj=document.subscribeform;
	var msg="Sorry, we cannot complete your request.\nKindly provide us the missing or incorrect information enclosed below.\n\n";
 	var error=false;
	var x,y,z;
	// if neither a valid email nor a valid phone number was entered:
	if(!checkemail_reg(obj.contact_email.value) ) 
	{ 
		msg+="- Please enter a valid email address.\n"; 
		y=1;
		error=true; 
	} 	
	if(error) 
	{
		alert(msg); 
		return false;
	} else 
	{	
		alert("Thank you! Your email has been added to the Small Business Funding Tips mailing list");
		return true;
	} 
}

//The following functions shows a warning message when text field is empty.
function isTextFieldEmpty(text_field_id,text_field_id_warning){
	if(document.getElementById(text_field_id).value == ""){
		//document.getElementById(text_field_id_warning).style.display = 'block';
		$j('#'+text_field_id_warning).fadeIn();
	}else{
		$j('#'+text_field_id_warning).fadeOut();
	}
}

//This function hides the warning message
function hideWarning(text_field_id_warning){
	//$("#"+text_field_id_warning).hide('slow');
	$j('#'+text_field_id_warning).fadeOut();
	//document.getElementById(text_field_id_warning).style.display = 'none';
}

function IsvalidPhoNumber(phone,contact_email,phone_warning,contact_email_warning){
	var contact_email = document.getElementById(contact_email).value
	var phone = document.getElementById(phone).value
	if(!validatePhoneNumber(phone)){//If not a valid phone number
		if(validateEmailAddress(contact_email)){
			swichButoons('active_submit_image','inactive_submit_image');//switch buttons
		}else{
			$j('#'+phone_warning).fadeIn();
			swichButoons('inactive_submit_image','active_submit_image');//switch buttons
		}
	}else{
		$j('#'+contact_email_warning).fadeOut();//Hide the email warning if was tirggered since the phone is correct 
		$j('#'+phone_warning).fadeOut();// Hide the warning message
		swichButoons('active_submit_image','inactive_submit_image');//switch buttons
	}
}

function IsvalidEmailAddress(contact_email,phone,phone_warning,contact_email_warning){
	var contact_email = document.getElementById(contact_email).value
	var phone = document.getElementById(phone).value
	if(!validateEmailAddress(contact_email)){//If not a valid phone number 
		if(validatePhoneNumber(phone)){
			swichButoons('active_submit_image','inactive_submit_image');//switch buttons
		}else{
			$j('#'+contact_email_warning).fadeIn();
			swichButoons('inactive_submit_image','active_submit_image');//switch buttons
		}
	}else{
		$j('#'+phone_warning).fadeOut();//Hide the email warning if was tirggered since the phone is correct 
		$j('#'+contact_email_warning).fadeOut();// Hide the warning message
		swichButoons('active_submit_image','inactive_submit_image');//switch buttons
	}
}


function validatePhoneNumber(phone_number){//validate phone number
	var regEx = /^\+?\(?([0-9]{1,4})\)?[-. ]?(([0-9]{3})[-. ]?)?([0-9]{3})[-. ]?([0-9]{3,5})$/;
	if(regEx.test(phone_number)){
		return true;
	}else{
		return false;//Invalid phone number
	}
}

function validateEmailAddress(email){//validate email address
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(!filter.test(email)){
		return false;
	}else{
		return true;
	}
}

//The following functions switches between the active and inactive button.
function swichButoons(make_active_id, make_inactive_id){
	$j('#'+make_active_id).fadeIn();
	$j('#'+make_inactive_id).hide();
}

function fireCheckEmail(){//Start the e-mail validator process
	if(document.getElementById('contact_email').value!=""){
		IsvalidEmailAddress('contact_email','phone','phone_warning','contact_email_warning');
	}else{
		hideWarning('contact_email_warning');//Hide warning if the email text is empty
		hideActiveButton();
	}
	//email_validator_counter = setTimeout("IsvalidEmailAddress('contact_email','phone','phone_warning','contact_email_warning')",100);
	setTimeout("fireCheckEmail()",100);
}


function fireCheckPhone(){//Start the e-mail validator process
	if(document.getElementById('phone').value!=""){
		 IsvalidPhoNumber('phone','contact_email','phone_warning','contact_email_warning');
	}else{
		hideWarning('phone_warning');//Hide the warning if the phone text is empty
		hideActiveButton();
	}
	//email_validator_counter = setTimeout("IsvalidEmailAddress('contact_email','phone','phone_warning','contact_email_warning')",100);
	setTimeout("fireCheckPhone()",100);
}

//This functions hides the active button if bothe email box and phone box are empty
function hideActiveButton(){
	if(document.getElementById('contact_email').value == "" && document.getElementById('phone').value == ""){
		swichButoons('inactive_submit_image','active_submit_image');//switch buttons
	}
}


