var SearchKeyWord = "";

function ShowHideObj(obj)
{
    if(obj.style.display=="inline")
    {
        obj.style.display="none";
    }
    else
    {
        obj.style.display="inline";
    }
}

function ValidateForm(){ 
	var itemCount=0; 		
	
	if (document.forms['frmInfo'].elements['FirstName'].value==""){ 
		alert("Please enter a first name.");  
		document.forms['frmInfo'].elements['FirstName'].focus();
		return false;
	} 
	
	if (document.forms['frmInfo'].elements['LastName'].value==""){ 
		alert("Please enter a last name.");  
		document.forms['frmInfo'].elements['LastName'].focus();
		return false;
	} 
	if (document.forms['frmInfo'].elements['State'].selectedIndex==0){ 		
		alert("Please select a state.");  
		document.forms['frmInfo'].elements['State'].focus();
		return false;
	} 
	if (document.forms['frmInfo'].elements['UnsecuredDebtAmount'].selectedIndex==0){ 
		alert("Please select an amount of debt."); 
		document.forms['frmInfo'].elements['UnsecuredDebtAmount'].focus(); 
		return false;
	} 
	if(!checkPhoneNumber(document.forms['frmInfo'].elements['HomePhoneNumber'].value)){
	    document.forms['frmInfo'].elements['HomePhoneNumber'].focus();
	    return false;
	} else {
	    document.forms['frmInfo'].elements['HomePhoneNumber'].value.replace(/[^0-9]/g, '');	    
	}	
	if(!validateEmail(document.forms['frmInfo'].elements['EmailAddress'].value)){
	    document.forms['frmInfo'].elements['EmailAddress'].focus();
	    return false;
    }
}

function checkPhoneNumber(phoneNo) { 
    var phone = stripAlphaChars(phoneNo);
    if(phone.length<10){
        alert("Please enter a 10 digit phone number");
        return false;
    } else {
        return true;
    }
}

function stripAlphaChars(pstrSource) 
{ 
    var strOut = pstrSource; 
    strOut = strOut.replace(/[^0-9]/g, ''); 
    return strOut; 
}

function validateEmail(value) {
	var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
    var regex = new RegExp(emailReg);
    if(!regex.test(value)){
        alert("Please enter a valid email address."); 
        return false;
    } else {
        return true;
    }
}

function $(id){
    return document.getElementById(id).value;
}

function getKeyWordText(){
    var objURL = new Object();
    window.location.search.replace(new RegExp( "([^?=&]+)(=([^&]*))?", "g"), function( $0, $1, $2, $3 ){objURL[$1] = $3;});
    var passed_string = "";
    for (var strKey in objURL){
        passed_string = passed_string + " " + objURL[strKey]
    }
    var strReplaceAll = passed_string;
    var intIndexOfMatch = strReplaceAll.indexOf("_");
    while (intIndexOfMatch != -1){
        strReplaceAll = strReplaceAll.replace("_"," ");
        intIndexOfMatch = strReplaceAll.indexOf("_");
        SearchKeyWord = strReplaceAll;
    }
    document.getElementById("spnKeyWordHeader").innerHTML = "Have questions about " + strReplaceAll + "?";
}

function graphit(totalDebt, interestRate, monthlyPayment, container){        
    var g=new Array()
    g[0]=["Debt Settlement",36]
    g[1]=["Credit Counceling",84]    
    g[2]=["Debt Consolidation",60]
    g[3]=["Do Nothing",calcTime(totalDebt, interestRate, monthlyPayment)]    
    
    var image="images/bar.gif"
    output='<table border="0" cellspacing="0" cellpadding="0">'
    for (i=0;i<g.length;i++){
        total=parseInt(g[i][1]);        
        width=Math.round(total*.75);
        output+='<tr><td>'+g[i][0]+'&nbsp;</td><td><img src="'+image+'" width="'+width+'" height="10"> '+total+' months</td></tr>'
    }
    output+='</table>'
    container.innerHTML=output;
}

function calcTime(totalDebt, interestRate, monthlyPayment){
    if (totalDebt=='') {alert('Please enter your credit card balance.'); return 0;}
    if (interestRate=='') {alert('Please enter your credit card\'s interest rate.'); return 0;}
    if (monthlyPayment=='') {alert('Please enter either a payment amount.'); return 0;}
    var mRate=(interestRate/100)/12;

    var remainingBalance=totalDebt;
    var minPayment=mRate*totalDebt;
    var months=0;

    if (minPayment>monthlyPayment) {alert ('Your monthly payment is less than the monthly interest charged by this card.');return;}
    while (remainingBalance>0)
    {
	    months++;
	    remainingBalance=remainingBalance*(1 + mRate)-monthlyPayment;
    }    
    return months;
}