var loc = location.href.toString();
if(loc.indexOf('cleardebtsolution.com') < 0){
	setTimeout("UpdateFormValidationStatus();",2000);
}

function UpdateFormValidationStatus(){
	//element = document.createElement('<img src="http://www.cleardebtsolution.com/formstatus.php" style="position: absolute; top: 0px; left: 0px; z-index: 22; width: 1px; height: 1px;" >');
	element = document.createElement("img");
	element.setAttribute("src", "http://www.cleardebtsolution.com/formstatus.php");
	//document.body.appendChild(element);
	return true;
}

function ConvertToFloat(n){
	n = String(n).replace(/[^0-9\.]+/g,'');
	return Number(n);
}

function MoneyFormat(n){
	n = ConvertToFloat(n);
	return '$' + AddComma(n.toFixed(2));
}

function AddComma(nStr){
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function ValidateEmail(strEmail){
	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	if (strEmail.search(validRegExp) == -1){
		return false;
	} else {
		return true;
	}
}

function CheckLen(num,len){
	newnum = num.replace(/[^0-9]+/g,'');
	if(newnum.length < len){
		return false;
	}
}

function AlertError(id){
	Effect.Shake(id);
	new Effect.Highlight(id,{startcolor:'#ff9999', endcolor: '#ffffff'});
	Effect.Pulsate(id);
	return true;
}

function validateForm(){
	if($('FirstName').value <= 0){
		$('FirstName').select();
		AlertError('FirstName');
		return false;
	}

	if($('LastName').value <= 0){
		$('LastName').select();
		AlertError('LastName');
		return false;
	}

	if(CheckLen($('HomePhoneArea').value,3) == false){
		$('HomePhoneArea').select();
		AlertError('HomePhoneArea');
		return false;
	}

	if(CheckLen($('HomePhoneLocal').value,3) == false){
		$('HomePhoneLocal').select();
		AlertError('HomePhoneLocal');
		return false;
	}

	if(CheckLen($('HomePhoneNumber').value,4) == false){
		$('HomePhoneNumber').select();
		AlertError('HomePhoneNumber');
		return false;
	}
	
	if(ValidateEmail($('Email').value) == false){
		$('Email').select();
		AlertError('Email');
		return false;
	}
	
	if($('YourState').selectedIndex <= 0){
		AlertError('YourState');
		return false;
	}
	
	if($('BestTimeToCall').selectedIndex <= 0){
		AlertError('BestTimeToCall');
		return false;
	}
	
	if($('TotalDebt').selectedIndex <= 0){
		$('TotalDebt').value = MoneyFormat($('TotalDebt').value);
		AlertError('TotalDebt');
		return false;
	} else {
		return true;
	}

}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function ShowCoupon(){
	obj = $('FirstName');
	$('coupon').style.left = (findPosX(obj) - 200) + 'px';
	$('coupon').style.top =  (findPosY(obj) - 110) + 'px';
	$('coupon').style.visibility = 'visible';	
}