// JavaScript Document
// Cookie related
function setCookie(c_name,value,expiredays){				 
	var exdate=new Date();
	exdate.setTime(exdate.getTime()+expiredays*24*60*60*1000);
	
	var cookie_str = c_name+ "=" +escape(value)+ ";path=/" + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	document.cookie= cookie_str;
}

function getCookie(c_name){
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){ 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
}   

function del_cookie(name) {
	document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
}			
// End of Cookie			

function getURLParam(name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	   var pattern = "[\\?&]"+name+"=([^&#]*)";
	   var regex = new RegExp(pattern);
	   var aResult = regex.exec(window.location.href);
	   if ( aResult == null ) {
		return "";
	} else {
		return aResult[1];
	   }
}


// hide or show DIV
function toggleDiv(divID) {
	var current_display= document.getElementById(divID).style.display;
	if (current_display == 'block') {
		document.getElementById(divID).style.display = 'none';
	}
	else {
		document.getElementById(divID).style.display = 'block';
	}
}

function showDiv(divID) {
		document.getElementById(divID).style.display = 'block';
}

function hideDiv(divID) {
	document.getElementById(divID).style.display = 'none';
}

// Clear Radio buttons checked
function clearRadios(radioName){ 
 var rads=document.getElementsByName(radioName); 
 var rads_ln=rads.length; 
 for(var i=0;i<rads_ln;i++){ 
 rads[i].checked=false; 
 } 
} 

// get which radio button is checked
function getRadioValue(form,radioName)
{
	for(i=0;i<form[radioName].length;i++)
	{
	if (form[radioName][i].checked == true)
	return form[radioName][i].value;
	}
}

// Show or Disable Field
function toggleDisableField(thisElement, toggleElementName ) {
	if (thisElement.checked) {
		document.getElementById(toggleElementName).disabled = false;
	}
	else {
		document.getElementById(toggleElementName).disabled = true;
	}
}


// Validating StockBroking Agreement Form
function validateAgreementForm() {
	if ((document.agreementForm.AccessAustralia.checked != true) || (document.agreementForm.AgreeTerms.checked != true)){
		document.getElementById('errorPanel').style.display = 'block';
		return false;
	} else {
		var formURL='';
		
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('!');
		for(var i = 0; i < hashes.length; i++) {
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		
		if (hash[0] == "fn") {
			document.agreementForm.action="http://"+window.location.hostname+"/stockbroking/content/products_features/agreement_redirect.jsp?fn="+hash[1]; 
			//window.location='http://'+window.location.hostname+'/stockbroking/content/products_features/agreement_redirect.jsp'; 
		} else {
			document.agreementForm.action="http://"+window.location.hostname+"/stockbroking/content/products_features/agreement_redirect.jsp?dp="+hash[1]; 
		}
		
		return true;
	}
}

// Open Forms in New Window
function openNewWindow(winURL, width, height) {
 var winHeight = (typeof(height) == 'undefined' ? 700 : height);
 var winWidth = (typeof(width) == 'undefined' ? 700 : width);
 var newWindow = window.open(winURL,'newwin','toolbar=0,location=0,directories=0,menubar=0,scrollbars=1,resizable=1, width='+winWidth+', height='+winHeight);
 newWindow.focus();
 return;
 }

// Toggle expand/collapsed icon
function toggleExpandIcon(anchorID, color) {
	var current_class= document.getElementById(anchorID).className;
	if (color == null) {
		// for normal light blue expand/collapsed icon
		if (current_class == 'collapsed') {
			document.getElementById(anchorID).className = 'expanded';
		}
		else {
			document.getElementById(anchorID).className = 'collapsed';
		}
	}
	else {
		// for specific color expand/collapsed icon
		if (current_class == 'collapsed-'+color) {
			document.getElementById(anchorID).className = 'expanded-'+color;
		}
		else {
			document.getElementById(anchorID).className = 'collapsed-'+color;
		}
	}
}
