/*
    Author          : Kobus du Toit
    Date Created    : 10-Sep-2007
    Last Changed By : Stefan Kruger
    Date Changed    :
    11-Sep-2007 - SG
    13-Dec-2007 - SK - Franchise Enquiry
    
*/

/**/


function submitFranchiseEnquiry(page,form){
	var data = fetchInputFormData(form);
	//alert(page + '\n\n' + data);
	hiddenSubmit(page,data,alertHiddenResponse,0,null,null,true);
	getHomePage();
}

var Mode = 'Start';
var Count = 0;
var t;

function resetTop10Count(){
	Count = 0;
	Mode = "Start";
}

function showimage(imagepath, counter){
    Count = counter;
    var name = "txtPID" + Count;
    var PropertyID = document.getElementById(name).value;
    //alert(PropertyID);
    document.frmHome.PropID.value = PropertyID;
	//document.all.item('imgLand').src = imagepath;
	document.getElementById('imgLand').src = imagepath;
}

function ImageChange(){
	try {
		var maxcount = document.getElementById("frmHome").propertycount.value;
        if(Mode == 'Start'){
			//alert(maxcount);
	        Count++;
	        if (Count > maxcount) Count = 1; //1;
	        
	        var name = "txtPID" + Count;
	        //alert(name);
	        var PropertyID = document.getElementById(name).value;
	        //alert(PropertyID);
	        document.frmHome.PropID.value = PropertyID;
	        if (Count > maxcount) Count = 1; //1;
		    var Path = (eval("document.getElementById('txtPath'+Count)").value)
		    document.getElementById('imgLand').src = Path;
		    t = setTimeout("ImageChange();",5000);
		}
	}
	catch(ex){/*alert(ex.message)*/}
}

function ChangeMode(PicMode){
	Mode = PicMode
	if (Mode == 'Stop'){
		clearTimeout(t);
	}
	else if(Mode == 'Start'){
		t = setTimeout("ImageChange();",5000);   
	} 
}

function loadCurrentProperty(){
	var propertyid = document.frmHome.PropID.value;
	//alert(propertyid);return;
    if (propertyid != "") {
		checkAction(5,1,1,'frmHome',false,null,true);
    }
    else {
		alert("No property value found.")
    }
}


///////////////////////////////////////////////////////////
//Bond calculator
///////////////////////////////////////////////////////////

function loadStaticContent(page){
	contentHider();
	//genericSubmit(page,data,preload,postload,div,whenDoneRunThis,LoaderToUse)
	genericSubmit(page,null,null,null,"topcontent",completedSingleLoad,regularloadingstring);
	//var data ='action=showpage'
	//hiddenSubmit(page, data, completedSingleLoad, 1, 'topcontent', null, false);
	
}

function compute() {
	var form = document.getElementById("frmBondCalc");
    var x = form.txtLoanAmount.value.replace(".00","").replace(",","").replace(",","").replace("R","") - form.txtDeposit.value.replace(".00","").replace(",","").replace(",","").replace("R","");
    var y = form.txtRate.value;
    var z = form.txtTerm.value;

    if ((x=="") || (y=="") || (z=="")) {
        alert("Please make sure that all fields above have been filled in before continuing");
        return;
    }
    var month = eval(pmt(x,(y*.01)/12,z*12));
    var fnight = eval(pmt(x,(y*.01)/26,z*26));
    var week = eval(pmt(x,(y*.01)/52,z*52));
    form.txtMonthly.value = currencyFormatter(rounding(month));
}

function pmt(ppl,rate,mths) {
    var x = eval((ppl*rate)/(eval(1-Math.pow(eval(rate+1),eval(0-mths)))));
    return x;
}

function rounding(value) {
    var newvalue = value * 100;
    newvalue = Math.round(newvalue) ;
    var strOutput = newvalue.toString() ;
    var len = strOutput.length ;
    return strOutput.substring(0, len - 2) + "." + strOutput.substring((len - 2), len);
}

function nper() {
	var form = document.getElementById("frmBondCalc");
    var fv = 0;
    var origterm = parseFloat(form.txtTerm.value);
    var origpay = parseFloat(form.txtMonthly.value.replace(".00","").replace(",","").replace(",","").replace("R",""));
    var extpay = parseFloat(form.txtExtra.value.replace(".00","").replace(",","").replace(",","").replace("R",""));
    var pv = parseFloat(form.txtLoanAmount.value.replace(".00","").replace(",","").replace(",","").replace("R","")-form.txtDeposit.value.replace(".00","").replace(",","").replace(",","").replace("R",""));
    var per = 12;
    var pv = -pv;
    var pay = origpay + extpay;
    var rate = form.txtRate.value;

	if (form.txtExtra.value == "") {
		alert("Please enter the extra monthly amount you would like to pay off on your loan.");
		return;
	}
	else {
		rate = eval((rate)/(per * 100));
		var nper_value;
		if (rate == 0){
			nper_value = - (fv + pv)/pay;
		}
		else {
			nper_value = Math.log((-fv * rate + pay)/(pay+ rate * pv))/ Math.log(1 + rate);
		}
        var new_nper = rounding(nper_value);
        var orig_total_amt = origpay * origterm * 12;
        var new_total_amt = pay * new_nper;
        var save = orig_total_amt - new_total_amt;
        form.txtNewTerm.value = rounding(nper_value/12);
        form.txtSaving.value = currencyFormatter(Math.round(save));
	}
}

///////////////////////////////////////////////////////////
//Scrolling Footer
///////////////////////////////////////////////////////////
var footermenuwidth = 500;
var footermenuspeed = 4;
var footeractualwidth = -1;
var footerlefttime = null,footerrighttime = null;

if (!(isie)) {
	footermenuspeed = 3*footermenuspeed;
}

function setFooterScrollable(){
	var tempDiv = addTempTabObject("footerScrollTempDiv");
	var footerScrollDiv = document.getElementById("footerscrollcontent")
	tempDiv.innerHTML = footerScrollDiv.innerHTML;
	footeractualwidth = tempDiv.offsetWidth;
	//alert(footeractualwidth);
	if (footeractualwidth > footermenuwidth) {
		showFooterScrollArrows(true);
	}
	else {
		showFooterScrollArrows(false);
	}
}

function showFooterScrollArrows(display){
	try {
		if (display) {
			if (document.getElementById("footerleftarrow") && document.getElementById("footerrightarrow")){
				document.getElementById("footerleftarrow").style.visibility = "visible";
				document.getElementById("footerrightarrow").style.visibility = "visible";
			}
		}
		else {
			if (document.getElementById("footerleftarrow") && document.getElementById("footerrightarrow")){
				document.getElementById("footerleftarrow").style.visibility = "hidden";
				document.getElementById("footerrightarrow").style.visibility = "hidden";
			}
		}
	}
	catch (ex){
		alert("Error : Occured during hiding footer arrows.");
	}
}


function moveFooterLeft(divisionToMove) {
	var tabs = document.getElementById(divisionToMove);
	try {
		var left = parseInt(tabs.style.left);
		//alert("Left : " + left +"\nIntLeft :" + parseInt(left) + "\nDifference : " + (menuwidth - actualwidth) + "\nActual Width : " + actualwidth + "\nMenu Width : " + menuwidth);
		if (left > (footermenuwidth - footeractualwidth)){
			tabs.style.left = (left - footermenuspeed);
		}
		footerlefttime = window.setTimeout(function() {
			moveFooterLeft(divisionToMove);
		},5);
	}
	catch (ex){
		alert("Error scrolling left :\n" + ex.message);
	}
	finally {
		left = null;
	}
	tabs = null;
}

function moveFooterRight(divisionToMove){
	var tabs = document.getElementById(divisionToMove);
	try {
		var left = parseInt(tabs.style.left);
		//alert("Left : " + left +"\nIntLeft :" + parseInt(left));
		if (left < 0){
			tabs.style.left = (left + footermenuspeed);
		}
		footerrighttime = window.setTimeout(function (){
			moveFooterRight(divisionToMove);
		},5);
	}
	catch (ex){
		alert("Error scrolling right :\n" + ex.message);
	}
	finally {
		left = null;
	}
	tabs = null;
}

function stopFooterScrollLeft(){
	try {
		clearTimeout(footerlefttime);
	}
	catch (ex){
		alert("Error clearing left timeout:\n" + ex.message);
	}
}

function stopFooterScrollRight(){
	try {
		clearTimeout(footerrighttime);
	}
	catch (ex){
		alert("Error clearing right timeout:\n" + ex.message);
	}
}

function changeTab_Search(tab){



	switch(tab)
	{
		case "Tab1":
		  document.getElementById('Tab1').className='tab_link_on';
		  document.getElementById('Tab2').className='tab_link_off';
		  document.getElementById('Tab3').className='tab_link_off';
		  document.getElementById('Tab4').className='tab_link_off';
		  document.getElementById('divbackground').className='tab_bg_1';
  		break;    
		
		case "Tab2":
		  document.getElementById('Tab1').className='tab_link_off';
		  document.getElementById('Tab2').className='tab_link_on';
		  document.getElementById('Tab3').className='tab_link_off';
		  document.getElementById('Tab4').className='tab_link_off';
		  document.getElementById('divbackground').className='tab_bg_2';
  		break;    
  		
		case "Tab3":
		  document.getElementById('Tab1').className='tab_link_off';
		  document.getElementById('Tab2').className='tab_link_off';
		  document.getElementById('Tab3').className='tab_link_on';
		  document.getElementById('Tab4').className='tab_link_off';
		  document.getElementById('divbackground').className='tab_bg_3';
  		break;    

		case "Tab4":
		  document.getElementById('Tab1').className='tab_link_off';
		  document.getElementById('Tab2').className='tab_link_off';
		  document.getElementById('Tab3').className='tab_link_off';
		  document.getElementById('Tab4').className='tab_link_on';
		  document.getElementById('divbackground').className='tab_bg_4';
  		break;    
  		
  	case "Tab5":
		  document.getElementById("topcontent").innerHTML = "test";
		  document.getElementById("bottomcontent").innerHTML = "";
		  //return;
  		break;    

		default:
			break;
  
	}
	
/*	

 var xmlHttp;
  
  try
    {    // Firefox, Opera 8.0+, Safari    
    	xmlHttp=new XMLHttpRequest();    
    }
  catch (e)
    {    // Internet Explorer    
    try
      {      
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
      }
    catch (e)
      {     
       try
        {        
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
        }
      catch (e)
        {        
        alert("Your browser does not support AJAX!");        
        return false;        
        }      
      }    
    }  

	xmlHttp.onreadystatechange=function()
    {
  	if(xmlHttp.readyState==4)
    	{
		//alert(xmlHttp.responseText);
		document.getElementById('SearchForm').innerHTML=xmlHttp.responseText;
    	}
    }
    
	var page
	
	page = "content/OfficeChangeTab.asp?action=" + tab;
	
    xmlHttp.open("GET",page,true);
    xmlHttp.send(null);

*/


	var alertxml = function(oXML)
		{
			var returnVal = oXML.responseText;

			if (returnVal.substr(0,5).toLowerCase() == "error")
			{
				alert (returnVal);
				return;
  			}
			else
			{
			if ((tab == 'Tab5') || (tab == 'Tab6') || (tab == 'Tab7'))
			{
				document.getElementById("topcontent").innerHTML = returnVal;
				//alert(tab);
			}
			else
 				document.getElementById('SearchForm').innerHTML=returnVal;
 			
			//					document.write(returnVal);
				return;
			}
		}
       
       var page;
       var data;
       
		page = "content/OfficeChangeTab.asp"
		data = "action=" + tab;
		hiddenSubmit(page,data, alertxml, 0, null, null, false);
		
}

// new section for tab search


function GotoNewCountryPage(Mode, FormName) //checkAction(1,3,1,null,false,null,false)
{
	var newurl;
	
	newurl = document.getElementById('country').value;
	//alert(newurl);
	window.open (newurl,"mywindow");
	//GotoSearchResultPageAgent(Mode, FormName);
}

function GotoNewPDFPage() //checkAction(1,3,1,null,false,null,false)
{
	var newurl;
	
	newurl = 'content/Download.pdf';
	//alert(newurl);
	window.open (newurl,"mywindow");
	//GotoSearchResultPageAgent(Mode, FormName);
}

function ValidateTabSearch() {

if (document.forms.TabSearch.location.value == '' && document.forms.TabSearch.name.value == '') 
        {
        alert("Please enter either location or name or both");
        return false;
        }
        return true;
 }
 
 
function GoToSub(subname,subid){



	var alertxml = function(oXML)
		{
			var returnVal = oXML.responseText;

			if (returnVal.substr(0,5).toLowerCase() == "error")
			{
				alert (returnVal);
				return;
  			}
			else
			{
			
				document.getElementById("topcontent").innerHTML = returnVal;
				//alert(tab);
				return;
			}
			
		}
       
       var page;
       var data;
       
		page = "content/Downloadsub2.asp"
		data = "subname=" + subname + "&subid=" + subid;
		hiddenSubmit(page,data, alertxml, 0, null, null, false);
		
}

function GoToMain(){



	var alertxml = function(oXML)
		{
			var returnVal = oXML.responseText;

			if (returnVal.substr(0,5).toLowerCase() == "error")
			{
				alert (returnVal);
				return;
  			}
			else
			{
			
				document.getElementById("topcontent").innerHTML = returnVal;
				//alert(tab);
				return;
			}
			
		}
       
       var page;
       var data;
       
		page = "content/Download.asp"
		data = "";
		hiddenSubmit(page,data, alertxml, 0, null, null, false);
		
}

function SaveOnlineApplication()
{
	var page;
    var data;
    var AfterAdd = function(oXML) 
    {			
        var result = oXML.responseText;
        //document.getElementById("topcontent").innerHTML = result;
        alert (result);
		//prompt('',result);
        regularCursor();

	    if (result.substr(0,5).toLowerCase() == "error")
            {
	            alert(result);
	            return;
            }
     }       
	if (ValidateOnlineApp())
	{
		page = "content/franchise_online_application_process.asp";
		data = "action=saveapplication&" + fetchInputFormData('frmOnlineApplication'); ;
		hiddenSubmit(page,data, AfterAdd, 0, null, null, false);
	}
}

function ValidateOnlineApp()
{
	if(document.getElementById('Title').value == '')
	{
		alert('Please enter valid Title for the application.')
		document.getElementById('Title').focus();
		return false;
	}
	else if(document.getElementById('Surname').value == '')
	{
		alert('Please enter valid Last Name for the application.')
		document.getElementById('Surname').focus();
		return false;
	}
	else if(document.getElementById('FirstName').value == '')
	{
		alert('Please enter valid First Name for the application.')
		document.getElementById('FirstName').focus();
		return false;
	}
	else if(document.getElementById('AddressDesc1').value == '')
	{
		alert('Please enter valid Address Description 1 for the application.')
		document.getElementById('AddressDesc1').focus();
		return false;
	}
	else if(document.getElementById('PostalCode').value == '')
	{
		alert('Please enter valid Postal Code for the application.')
		document.getElementById('PostalCode').focus();
		return false;
	}
	else if(document.getElementById('CellNumber').value == '')
	{
		alert('Please enter valid Cell Number for the application.')
		document.getElementById('CellNumber').focus();
		return false;
	}
	else if(document.getElementById('Email').value == '')
	{
		alert('Please enter valid Email for the application.')
		document.getElementById('Email').focus();
		return false;
	}
	else if(document.getElementById('Email').value != '')
	{
		if (!isValidEmail(document.getElementById('Email').value))
		{
			alert('Please enter valid Email for the application.')
			document.getElementById('Email').focus();
			return false;
		}
		else
		{
			return true;	
		}
	}	
	else 
	{
		return true;
	}	
}

function saveGenquriy()
{
  if(document.getElementById("name").value == "")
    {
        alert('Please Enter Your Name!');
        document.getElementById("name").focus();
        return false;
    }
  if(document.getElementById("email").value == "")
    {
        alert('Please Enter valid email!');
        document.getElementById("email").focus();
        return false;
    }	
  if(document.getElementById("hphone").value == "")
    {
        alert('Please Enter Your Phone Number!');
        document.getElementById("hphone").focus();
        return false;
    }
  if(document.getElementById("comments").value == "")
    {
        alert('Please Enter Your Comments!');
        document.getElementById("comments").focus();
        return false;
    }
    else
    {
    var AfterAdd = function(oXML) 
    {			
        var result = oXML.responseText;
        regularCursor();

	    if (result != "")
            {
	            alert(result);
	            return;
            }
     }
        var Add = "Add";
        var data = "action=" + Add + "&" + fetchInputFormData('genearlenquriy');
        //alert(data);
       hiddenSubmit('/content/GeneralEnquirries.asp',data, AfterAdd, 0, null, null,true);
       //checkAction(42,116,1,'frmPropDetail',false,null,false);
       //alert ("Your data has been deleted!")
   }
}

function saveFranchiseGenquriy()
{
    var AfterAdd = function(oXML) 
    {			
        var result = oXML.responseText;
        regularCursor();

	    if (result != "")
            {
	            alert(result);
	            return;
            }
     }
	 
   if(document.getElementById("eMail").value == "")
    {
        alert('Please Enter valid email!');
        document.getElementById("eMail").focus();
        return false;
    }	
	else
	{
        var Add = "Add";
        var data = "action=" + Add + "&" + fetchInputFormData('franchiseenquriy');
        //alert(data);
       hiddenSubmit('/content/franchiseenquiry.asp',data, AfterAdd, 0, null, null,true);
	   
	}
       //checkAction(42,116,1,'frmPropDetail',false,null,false);
       //alert ("Your data has been deleted!")

}


function InitLightBox(){
	$('a[rel*=lightbox]').lightBox(); // Select all links that contains lightbox in the attribute rel
}

function InitThickBox(){
	//$('a.thickbox').thickbox(); // Select all links that contains lightbox in the attribute rel
	tb_reinit();
}