var xmlRequest;

function CreateXMLHttpRequest() {    	
    try {		
        //this work if XMLHttpRequest is part of Javascript
        //alert('Fire');
		xmlRequest = new XMLHttpRequest(); 
	
    } catch (err) {
		 // Internet Explorer
		 //alert("IE");
		xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");	
		//xmlRequest=new ActiveXObject("Msxml2.XMLHTTP");
    }	
}

function calculateRate(lender_id){
	if (confirm("Are you sure you want to Calculate this lender?") == true) {
        var url = "/_cms/main_rate/calculate.php?lender_id=" + lender_id;  
		//alert(url);
		xmlRequest.open("GET", url, false);
		xmlRequest.onreadystatechange = returnCalculateRate;
		xmlRequest.send();
	} 	
}

//Return the Category referral drop box
function returnCalculateRate() {
    //check if the response was received successfully
    if (xmlRequest.readyState == 4) {
        if (xmlRequest.status == 200) {           
            var reponse = xmlRequest.responseText;
			alert(reponse);            		
        }
    }
}


function getChangeRate(id,rate){	
    if (confirm("Are you sure you want to updated this rate?") == true) {
        var url = "/_cms/rate/edit_rate.php?id=" + id + "&rate=" + encodeURIComponent(rate); 
		//url = encodeURIComponent(url);
		alert(url);
		xmlRequest.open("GET", url, false);
		xmlRequest.onreadystatechange = returnRate;
		xmlRequest.send();
    } 
}

function getProductName(id,name){	
    if (confirm("Are you sure you want to updated this product name?") == true) {
        var url = "/_cms/main_rate/change_product_name.php?id=" + id + "&value=" + name + "";  
		//alert(url);
		xmlRequest.open("GET", url, false);
		xmlRequest.onreadystatechange = returnProductName;
		xmlRequest.send();
    } 
}

//Return the Category referral drop box
function returnProductName() {
    //check if the response was received successfully
    if (xmlRequest.readyState == 4) {
        if (xmlRequest.status == 200) {           
            var reponse = xmlRequest.responseText;
			//alert(reponse);            		
			alert("Product Name Updated Successfully!");            		
        }
    }
}

//Return the Category referral drop box
function returnRate() {
    //check if the response was received successfully
    if (xmlRequest.readyState == 4) {
        if (xmlRequest.status == 200) {           
            var reponse = xmlRequest.responseText;
			alert("Rate Updated Successfully!");            		
        }
    }
}

function getChangeCalculatedRate(id,rate){	
    if (confirm("Are you sure you want to updated this rate?")) {
        var url = "/_cms/main_rate/edit_rate.php?id= " + id + "&rate=" + rate;  
		//url = encodeURIComponent(url);
		// alert(url);
		xmlRequest.open("GET", url, false);
		xmlRequest.onreadystatechange = returnCalculatedRate;
		xmlRequest.send();
    } 
}

//Return the Category referral drop box
function ClientPaidInsurance() {
    //check if the response was received successfully
    if (xmlRequest.readyState == 4) {
        if (xmlRequest.status == 200) {           
            var reponse = xmlRequest.responseText;
			alert("Client Paid Insurance Checkbox Updated Successfully!");            		
        }
    }
}

function getClientPaidInsurance(id, isChecked){	
    
        var url = 'change_client_paid_insurance.php?id=' + id + "&isChecked=" + isChecked;
		xmlRequest.open("GET", url, false);
		xmlRequest.onreadystatechange = ClientPaidInsurance;
		xmlRequest.send();
   
}

//Return the Category referral drop box
function returnCalculatedRate() {
    //check if the response was received successfully
    if (xmlRequest.readyState == 4) {
        if (xmlRequest.status == 200) {           
            var reponse = xmlRequest.responseText;
            alertify.set('notifier','position', 'top-right');
			alertify.success('Rate Updated Successfully!');           		
        }
    }
}


function getIsCalculated(id, isChecked){	
    //if (confirm("Are you sure you want to updated this rate?") == true) {
        var url = 'change_is_calculate.php?id=' + id + "&isChecked=" + isChecked;
		xmlRequest.open("GET", url, false);
		xmlRequest.onreadystatechange = returnIsCalculated;
		xmlRequest.send();
   // } 
}

//Return the Category referral drop box
function returnIsCalculated() {
    //check if the response was received successfully
    if (xmlRequest.readyState == 4) {
        if (xmlRequest.status == 200) {           
            var reponse = xmlRequest.responseText;
			alert("Calculated Checkbox Updated Successfully!");            		
        }
    }
}

function getIsInsured(id, isChecked){	
    //if (confirm("Are you sure you want to updated this rate?") == true) {
        var url = 'change_is_insured.php?id=' + id + "&isChecked=" + isChecked;
		xmlRequest.open("GET", url, false);
		xmlRequest.onreadystatechange = returnIsInsured;
		xmlRequest.send();
   // } 
}

//Return the Category referral drop box
function returnIsInsured() {
    //check if the response was received successfully
    if (xmlRequest.readyState == 4) {
        if (xmlRequest.status == 200) {           
            var reponse = xmlRequest.responseText;
			alert("Insured Checkbox Updated Successfully!");            		
        }
    }
}

function getCity(){
	//alert("Testing");	
	var provinceID = document.getElementById("Province").value;  	
	//alert(provinceID);	
	var url = "/_widgets/city_search/return_city.php?id=" + provinceID;   
	xmlRequest.open("GET", url, false);
	xmlRequest.onreadystatechange = returnCity;
	xmlRequest.send();
		
}

//Return the Category referral drop box
function returnCity() {
    //check if the response was received successfully
    if (xmlRequest.readyState == 4) {
        if (xmlRequest.status == 200) {
            var lbl = document.getElementById("city");
            var reponse = xmlRequest.responseText;
            lbl.innerHTML = reponse;		
        }
    }
}

//hide 
function hideReferral(){		
	try {		
       //hide_referral 	= document.frmApplication.hide_referral;	
		div3Year = document.getElementById("referralsource");
		//hide
		div3Year.style.display = "none";		
    } catch (err) {
		
    }	
}

//show
function showReferral(){	
	//hide_referral 	= document.frmApplication.hide_referral;
		try {		
       div3Year = document.getElementById("referralsource");	
	//show
	div3Year.style.display = "block";			
    } catch (err) {
		//nothing
    }
		
		
}

//Populate the Category referral drop box
//=====================================================================
function searchReferral() {    
	//alert("Testing");	
	var CategoryId = document.getElementById("referralCategory").value;
    if(CategoryId == ""){
		hideReferral();
	}else{
		showReferral();
		//alert(CategoryId);	
		var url = "/_widgets/apply_now/apply_online/_getReferral.php?id=" + CategoryId;   
		xmlRequest.open("GET", url, false);
		xmlRequest.onreadystatechange = getReferralCategory;
		xmlRequest.send();
	}	
  }

//Return the Category referral drop box
function getReferralCategory() {
    //check if the response was received successfully
    if (xmlRequest.readyState == 4) {
        if (xmlRequest.status == 200) {
            var lbl = document.getElementById("temp_referral");
            var reponse = xmlRequest.responseText;
            lbl.innerHTML = reponse;		
        }
    }
}


function trim(strText) { 
	// this will get rid of leading spaces 
	while (strText.substring(0,1) == ' ') 
		strText = strText.substring(1, strText.length);
	
	// this will get rid of trailing spaces 
	while (strText.substring(strText.length-1,strText.length) == ' ')
		strText = strText.substring(0, strText.length-1);
	
	return strText;
}


function ValidateNumber(textbox){
	if(textbox.value != ""){
		if(isNaN(textbox.value)){
			alert("Please enter a number.");
			textbox.focus();
		}
	}
}


function ConfirmDelete(sName, sAdditionalText){
	var strConfirm = "Are you sure you want to delete '" + sName + "'?";
	if(sAdditionalText != ""){
		strConfirm += "\n" + sAdditionalText;
	}
	return confirm(strConfirm);
}


function IsValidEmail(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if(str.indexOf(at)==-1){ return false; }
	if(str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){ return false; }
	if(str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){ return false; }
	if(str.indexOf(at,(lat+1))!=-1){ return false; }
	if(str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){ return false; }
	if(str.indexOf(dot,(lat+2))==-1){ return false; }
	if(str.indexOf(" ")!=-1){ return false; }
	return true;
}




//lender logos on right side
function SwapLogo(){
	++currentLogo;
	if(currentLogo == numLogos){ currentLogo = 0; }
	
	var newImage = arrLenderLogos[currentLogo][0];
	var newWebsite = arrLenderLogos[currentLogo][1];
	var newName = arrLenderLogos[currentLogo][2];
	
	var str = "";
	if(newWebsite != ""){ str += "<a href='" + newWebsite + "' target='_blank'>"; }
	str += "<img src='/_documents/lender_logos/" + newImage + "' border='0' alt='" + newName + "'>";
	if(newWebsite != ""){ str += "</a>"; }
	
	document.getElementById("lender_logo_images").innerHTML = str;
	setTimeout("SwapLogo()", 3000);
}


function SwapHomepageImage(){
	return;
	++currentHomepageImage;
	if(currentHomepageImage == numHomepageImages){ currentHomepageImage = 0; }
	
	var newImage = arrHomepageImages[currentHomepageImage];
	
	var str = "";
	str += "<a href='/apply-now/'><img src='" + newImage + "' width='442' height='288' alt='' /></a>";
	//str += "<a href='/apply-now/'><img id='applyNow' src='/_templates/mb/images/body/slider/apply.png' alt='What are you going to do with the time and money you save?' title='What are you going to do with the time and money you save?' /></a>";
	

	document.getElementById("topImage").innerHTML = str;
	setTimeout("SwapHomepageImage()", 8000);
}

function SwapFirstHomepageImage(){
	setTimeout("SwapHomepageImage()", 8000);
}





//AJAX CODE
var xmlHttp; //used by synchronous function

var newHttpIndex = -1; //used by asynchronous function
var arrHttpObjects = new Array();
var elementToFill = new Array();
var replaceElementContents = new Array();

function GetXmlHttpObject(){
	var xmlHttp=null;
	
	try{ // Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}catch (e){ // Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	return xmlHttp;
}

function AddSynchronousAjaxHTML(url, el, replaceContents){
	//get html and put it in an element (it can replace the content of the element or append to it)
	//this one does a synchronous call
	xmlHttp = GetXmlHttpObject();
	if(xmlHttp != null){
		xmlHttp.open("GET", url, false);
		xmlHttp.send(null);
		returnedText = xmlHttp.responseText;
		
		if(replaceContents){
			document.getElementById(el).innerHTML = returnedText;
		}
		else{
			document.getElementById(el).innerHTML += returnedText;
		}
	}
}

function AddAjaxHTML(url, el, replaceContents){
	//get html and put it in an element (it can replace the content of the element or append to it)
	newHttpIndex++;
	//WriteMessage("AddAjaxHTML IS RUNNING (" + newHttpIndex + ")");
	arrHttpObjects[newHttpIndex] = GetXmlHttpObject();
	if(arrHttpObjects[newHttpIndex] != null){
		elementToFill[newHttpIndex] = el;
		replaceElementContents[newHttpIndex] = replaceContents;
		arrHttpObjects[newHttpIndex].onreadystatechange = function () { stateChanged(newHttpIndex); };
		arrHttpObjects[newHttpIndex].open("GET", url, true);
		arrHttpObjects[newHttpIndex].send(null);
	}
}

function stateChanged(httpIndex){
	//used by AddAjaxHTML()
	//WriteMessage("stateChanged IS RUNNING, httpIndex: " + httpIndex + ", readyState: " + arrHttpObjects[httpIndex].readyState);
	if(arrHttpObjects[httpIndex].readyState == 4){
		var returnedText = arrHttpObjects[httpIndex].responseText;
		if(replaceElementContents[httpIndex]){
			document.getElementById(elementToFill[httpIndex]).innerHTML = returnedText;
		}
		else{
			document.getElementById(elementToFill[httpIndex]).innerHTML += returnedText;
		}
		
		arrHttpObjects[httpIndex] = null;
	}
}

//ADD Asset on online app
function addAsset() {     
	var url = "/_widgets/apply_now/apply_online/addAssetLiabilities.php?w=a";	
	xmlRequest.open("GET", url);	
    xmlRequest.onreadystatechange = getAsset;
    xmlRequest.send();
   }

//search articles
function getAsset() {
    //check if the response was received successfully
    if (xmlRequest.readyState == 4) {
        if (xmlRequest.status == 200) {
            var lbl = document.getElementById("addAsset");
            var reponse = xmlRequest.responseText;	
			$("#addAsset").append(reponse);			
            //lbl.innerHTML = lbl.innerHTML + '' + reponse;	
			//lbl.appendChild(reponse);		
        }
    }
}

//ADD Liabilities on Online application
function addLiabilities() {    
	//alert("Testing");
	//var title = document.title;  ;
	var url = "/_widgets/apply_now/apply_online/addAssetLiabilities.php?w=l";	 
    xmlRequest.open("GET", url);	
	xmlRequest.onreadystatechange = getLiabilities;
    xmlRequest.send();
   }

//search articles
function getLiabilities() {
    //check if the response was received successfully
    if (xmlRequest.readyState == 4) {
        if (xmlRequest.status == 200) {
            var lbl = document.getElementById("addLiabilities");
            var reponse = xmlRequest.responseText;
			//lbl.innerHTML = lbl.innerHTML + reponse		
			$("#addLiabilities").append(reponse);
        }
    }
}






