
registerOnClick();

function registerOnClick()
{
	// Usage in firefox
	if (typeof document.addEventListener != "undefined")
	{
		window.addEventListener("click", captureOnClick, false)
	} // Usage in IE
	else if (typeof document.attachEvent != "undefined")
	{
		document.attachEvent("onclick", captureOnClick);
	}
}

function captureOnClick( event )
{
	var t = getTarget( event );
	if( t.id == 'email' )
	{
		email_f = document.getElementById('email');
		if( email_f != null && typeof email_f != 'undefined' )
		{
			email_f.onclick = null;
		}
	}
}

function getTarget(x)
{
    x = x || window.event;
    return x.target || x.srcElement;
}




// affiliate confirmation
function validateAffForm(affForm) {
    if (affForm.name.value == "") {
        alert("All mandatory fields must be filled. Please try again.");
        return false;
    }
    if (affForm.email.value == "") {
        alert("All mandatory fields must be filled. Please try again.");
        return false;
    }
    if (affForm.emailRetype.value == "") {
        alert("All mandatory fields must be filled. Please try again.");
        return false;
    }
    if (affForm.email.value != affForm.emailRetype.value) {
        alert("The e-mail adresses you have entered do not match. Please try again.");
        return false;
    }
    if (affForm.country.value == "none") {
        alert("All mandatory fields must be filled. Please try again.");
        return false;
    }
    if (affForm.pass.value == "") {
        alert("All mandatory fields must be filled. Please try again.");
        return false;
    }
    if (affForm.passRetype.value == "") {
        alert("All mandatory fields must be filled. Please try again.");
        return false;
    }
    if (affForm.pass.value != affForm.passRetype.value) {
        alert("The passwords you have entered do not match. Please try again.");
        return false;
    }
    return true;
}

// format a number like this : 1,000,000
function addCommas(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 exitpage(msg, url)
{
        if(confirm(msg))
        {
//              window.open(url,'ExitPage');
                location.href = url;
        }
}

function bounceback(msg)
{
        alert(msg);
        history.back();
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
        if(!radioObj)
                return "";
        var radioLength = radioObj.length;
        if(radioLength == undefined)
                if(radioObj.checked)
                        return radioObj.value;
                else
                        return "";
        for(var i = 0; i < radioLength; i++) {
                if(radioObj[i].checked) {
                        return radioObj[i].value;
                }
        }
        return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
        if(!radioObj)
                return;
        var radioLength = radioObj.length;
        if(radioLength == undefined) {
                radioObj.checked = (radioObj.value == newValue.toString());
                return;
        }
        for(var i = 0; i < radioLength; i++) {
                radioObj[i].checked = false;
                if(radioObj[i].value == newValue.toString()) {
                        radioObj[i].checked = true;
                }
        }
}
//-->

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document;
if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array;
for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function cs_getParentClickable(obj, flag){
  for(var j=0; j<=5; j++){
    if(flag == 1){ if(obj && obj.nodeName.toUpperCase() == 'A'){ return obj; } } //look for <A> tag
    if(flag == 2){ if(obj && obj.nodeName.toUpperCase() != 'BODY' && obj.onclick){ return obj; } } //look onclick tag
    obj = obj.parentNode;
  }
  return null;
}

function cs_do_track(tg, delay, context){
  var url = null;
  var text = null;
    if(tg){
      tgP = cs_getParentClickable(tg, 1);
      if(tgP && tgP.nodeName.toUpperCase() == 'A'){ tg = tgP; }
      if(tg.nodeName.toUpperCase() == 'A'){//A tag - most popular case
        url = tg.href;
        if(tg.innerHTML){ text = tg.innerHTML; } //most browsers
        else if(tg.innerText){ text = tg.innerText; } //ie only
        else if(tg.text){ text = tg.text; } //mozilla only
        else{}
      }else if(tg.nodeName.toUpperCase() == 'INPUT' && tg.type.toUpperCase() == 'SUBMIT'){//form submit button
        url = tg.form.action;
        text = tg.value;
      }else{
       /* this eliminates all other onclick events */

      }
      if(url == null || text == null){ return true; } //both url and text must not be NULL
      cs_track_oc(text, url, delay, context);
     }
}

function cs_track_oc(text, url, delay, context){
    var now = new Date();
    if(context == false)
    {
        var trackURL = '/track.php' + '?u=' + url + '&te=' + text + '&loc=' + location;
        var x = new Image();
        x.src = trackURL;
    } else {
        var trackURL = '/trackreport.php' + '?u=' + url + '&te=' + text + '&loc=' + location;
        if(parent.top_page)
                parent.top_page.location=trackURL;
   }
    if(delay){ cs_pause(900); }
}

function cs_pause(numberMillis) {
  var now = new Date();
  var exitTime = now.getTime() + numberMillis;
  while(true){
    now = new Date();
    if(now.getTime() > exitTime){ return; }
  }
}

function cs_on_click(e){//left click
  var tg = null;
  if(e){ tg = e.target; }
  else{ tg = window.event.srcElement; }
  cs_do_track(tg, true, false);
}

function cs_on_context(e){//left click
  var tg = null;
  if(e){ tg = e.target; }
  else{ tg = window.event.srcElement; }
  cs_do_track(tg, true, true);
  return false;
}

//document.body.onclick = cs_on_click;
//if(parent.top_page)
//      document.body.oncontextmenu = cs_on_context;

function toggleBox(szDivID, iState) // 1 visible, 0 hidden
{
    if(document.layers)    //NN4+
    {
       document.layers[szDivID].visibility = iState ? "show" : "hide";
    }
    else if(document.getElementById)      //gecko(NN6) + IE 5+
    {
        var obj = document.getElementById(szDivID);
        obj.style.visibility = iState ? "visible" : "hidden";
    }
    else if(document.all)       // IE 4
    {
        document.all[szDivID].style.visibility = iState ? "visible" : "hidden";
    }
}
var ie=document.all;
var dom=document.getElementById;
var ns4=document.layers;
var calunits=document.layers? "" : "px";

var bouncelimit=16; //(must be divisible by 8)
var direction="up";

function initbox(Id)
{
        if (!dom&&!ie&&!ns4) return;
        crossobj=(dom)?document.getElementById(Id).style : ie? document.all.Id : document.Id;
        scroll_top=(ie)? truebody().scrollTop : window.pageYOffset;
        crossobj.left=-250+calunits;
        crossobj.top= (scroll_top + 10) + calunits;
        crossobj.visibility=(dom||ie)? "visible" : "show";
        dropstart=setInterval("dropin()",40);
}

function dropin()
{
        scroll_top=(ie)? truebody().scrollTop : window.pageYOffset;
        if (parseInt(crossobj.left)<90)
                crossobj.left=parseInt(crossobj.left)+20+calunits;
        else{
                clearInterval(dropstart);
                bouncestart=setInterval("bouncein()",50);
        }
}

function bouncein()
{
        crossobj.left=parseInt(crossobj.left)-bouncelimit+calunits;
        if (bouncelimit<0)
                bouncelimit+=8;

        bouncelimit=bouncelimit*-1;
        if (bouncelimit==0){
                clearInterval(bouncestart);
        }
}

function truebody()
{
        return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function popup() {
window.open( "popup.php", "Popup",
"status = 1, height = 600, width = 400, resizable = 0" )
}

// go to the specified page
// for onclick events
function gotoPage(url){
       window.location.href = url;
}

// function to replace rel="external" with target="_blank"
// to keep the html code valid XHTML Strict
function externalLinks( ) {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}

// clear all radio button
function clear_all_radios(){
    var lb = "\x5B";
    var rb = "\x5D"
    var elements = document.getElementsByTagName('input');
    var elnum;
    for(elnum=0;elnum<elements.length;elnum++){
        var el = eval("elements"+lb+"elnum"+rb);
        if ( el.type == 'radio') {
            el.checked = false;
        }
    }
}

/*check hostname for different areas*/
function check_hostname( _url ){
	if(window.location.hostname.match(/[a-zA-Z]+\.dev$/)){
		var _host_arr = window.location.hostname.split(/\./);
		_url = _url + '.' + _host_arr[2] + '.dev';
	}
	else if(window.location.hostname.match(/^devel\.www\./))
		_url = 'devel.www.' + _url;
	else if(window.location.hostname.match(/^qa\.www\./))
		_url = 'qa.www.' + _url;
	else if(window.location.hostname.match(/^stage\.www\./))
		_url = 'stage.www.' + _url;

	return _url;
}

/*contactus form check*/
function SubmitContact( ){
	var _email = document.getElementById('email');
	var _name = document.getElementById('name');
	var _message = document.getElementById('message');

	if(!MandatoryCheck(_name) || _name.value.length < 2){
		alert(err_messages_stack['name_validity']);
        return false;
	}
	if(!MandatoryCheck(_email) || !EmailCheck(_email)){
		alert(err_messages_stack['email_validity']);
        return false;
	}
	if(!MandatoryCheck(_message) || _message.value.length < 2){
		alert(err_messages_stack['message_validity']);
        return false;
	}
	return true;
}

/*unsubscribe fomr check*/
function SubmitUnsubscribe( )
{
	var _email = document.getElementById('email');
	var _lid = document.getElementById('lid');
	if(_lid == null || typeof _email=="undefined" )
	{
		if(_email==null || typeof _email=="undefined"){
		    _email = document.getElementById('emailField');
		    if(_email==null || typeof _email=="undefined"){
				return false;
		    }
		}
	}
	///mandatory
	if(!MandatoryCheck(_email)){
		alert(err_messages_stack['mandatory']);
		return false;
	}
	///email validity
	if(!EmailCheck(_email)){
		alert(err_messages_stack['email_validity']);
        return false;
	}
}

/*contactus form check*/
function SubmitAdvertiser( ){
	var _email = document.getElementById('email');
	var _lname = document.getElementById('lname');
	var _fname = document.getElementById('fname');

	if(!MandatoryCheck(_lname) || _lname.value.length < 2){
		alert(err_messages_stack['lname_validity']);
        return false;
	}
	if(!MandatoryCheck(_fname) || _fname.value.length < 2){
		alert(err_messages_stack['fname_validity']);
        return false;
	}
	if(!MandatoryCheck(_email) || !EmailCheck(_email)){
		alert(err_messages_stack['email_validity']);
        return false;
	}
	return true;
}

////////////////////////////////new way
/*mandatory checks*/
function MandatoryCheck(_elm){
	return trim(_elm.value) != '';
}

/*checking email validity*/
function EmailCheck(_elm)
{
	reg = new RegExp(/^([\w\-]+)(\.[\w\-]+)*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}$/);
	var email_addr = _elm.value.split(' ').join('');
	if( reg.exec(email_addr) == null )
	{
		new Ajax.Request('nve_log.html', {parameters: 'nvel=' + email_addr, asynchronous: true, method: 'get' });
		return false;
	}
	else
	{
		return true;
	}
}

/*checking terms status*/
function TermsCheck(_elm){
	return _elm.type == "checkbox" ? _elm.checked : true;
}

handleLedID = function(t){
	_ledid=t.responseText;
}

/* Download CasinoInstaller client */
function submitDownloadCasinoInstaller( _snippet )
{
	var _email = document.getElementById('email');
	if(_email==null || typeof _email=="undefined"){
	    _email = document.getElementById('emailField');
	    if(_email==null || typeof _email=="undefined"){
			return false;
	    }
	}
	///mandatory
	if(!MandatoryCheck(_email)){
		alert(err_messages_stack['mandatory']);
		return false;
	}
	///email validity
	if(!EmailCheck(_email)){
		alert(err_messages_stack['email_validity']);
        return false;
	}
	///terms status
	var _terms = document.getElementById('terms');
	if(_terms!=null && typeof _terms!="undefined"){
		if(!TermsCheck(_terms)){
			alert(err_messages_stack['terms']);
			return false;
		}
	}
	///create lead
	new Ajax.Request('createledid.html',{parameters: 'email=' + _email.value, asynchronous:false, method: 'get', onSuccess: handleLedID});
	///start download process via "hitsyndication" host
	if(_ledid!=0)
    	DownloadCasinoInstaller(_snippet, _ledid);
    else
		return false;

	return true;
}

/*submit download casino installer*/
var _ledid=0;
function Submitdownload(_casino){
	var _email = document.getElementById('email');
	if(_email==null || typeof _email=="undefined"){
	    _email = document.getElementById('emailField');
	    if(_email==null || typeof _email=="undefined"){
			return false;
	    }
	}
	///mandatory
	if(!MandatoryCheck(_email)){
		alert(err_messages_stack['mandatory']);
		return false;
	}
	///email validity
	if(!EmailCheck(_email)){
		alert(err_messages_stack['email_validity']);
        return false;
	}
	///terms status
	var _terms = document.getElementById('terms');
	if(_terms!=null && typeof _terms!="undefined"){
		if(!TermsCheck(_terms)){
			alert(err_messages_stack['terms']);
			return false;
		}
	}
	///create lead
	new Ajax.Request('createledid.html',{parameters: 'email=' + _email.value, asynchronous:false, method: 'get', onSuccess: handleLedID});
	///start download process via "hitsyndication" host
	if(_ledid!=0)
    	DownloadCasino(_casino, _ledid);
    else
		return false;

	return true;
}

/*submit download casino installer*/
var _ledid=0;
function SubmitOpenCasino(_casino){
	var _email = document.getElementById('email');
	if(_email==null || typeof _email=="undefined"){
	    _email = document.getElementById('emailField');
	    if(_email==null || typeof _email=="undefined"){
			return false;
	    }
	}
	///mandatory
	if(!MandatoryCheck(_email)){
		alert(err_messages_stack['mandatory']);
		return false;
	}
	///email validity
	if(!EmailCheck(_email)){
		alert(err_messages_stack['email_validity']);
        return false;
	}
	///terms status
	var _terms = document.getElementById('terms');
	if(_terms!=null && typeof _terms!="undefined"){
		if(!TermsCheck(_terms)){
			alert(err_messages_stack['terms']);
			return false;
		}
	}
	///create lead
	new Ajax.Request('createledid.html',{parameters: 'email=' + _email.value, asynchronous:false, method: 'get', onSuccess: handleLedID});
	///start download process via "hitsyndication" host
	if(_ledid!=0)
    	OpenCasino(_casino, _ledid);
    else
		return false;

	return true;
}

/* Download client installer */
function DownloadCasinoInstaller( _snippet, _lid )
{
	window.open('http://' + check_hostname('hitsyndication.com') +'/clientdownload.html?snippet='+_snippet+'&ledid='+_lid,"popDown","width=1,height=1,top=0,left=0");
}

/*direct casino download for links(anchors) to download casinos*/
function DownloadCasino( _casino, _lid )
{
	window.open('site_redirect.html?casino='+_casino+'&ledid='+_lid,"popDown","width=1,height=1,top=0,left=0");
}

/*open casino in new target*/
function OpenCasino( _casino, _lid )
{
	//_rand = Math.random();
	//_timestamp = new Date( ).getTime( )/1000;
	//_rand = (_rand * 100);
    window.open('site_redirect.html?casino='+_casino+'&ledid='+_lid,"Casino");
}

/*download installer for links(anchors) to download installer*/
function DownloadInstaller(_installer){
  window.open(_installer,"popDown","width=1,height=1,top=0,left=0");
  return;
}

function SubmitLead( _formname ){
	var _email = document.getElementById('email');
	if(_email==null || typeof _email=="undefined"){
	    _email = document.getElementById('emailField');
	    if(_email==null || typeof _email=="undefined"){
			return false;
	    }
	}
	///mandatory
	if(!MandatoryCheck(_email)){
		alert(err_messages_stack['mandatory']);
		return false;
	}
	///email validity
	if(!EmailCheck(_email)){
		alert(err_messages_stack['email_validity']);
        return false;
	}
	///terms status
	var _terms = document.getElementById('terms');
	if(_terms!=null && typeof _terms!="undefined"){
		if(!TermsCheck(_terms)){
			alert(err_messages_stack['terms']);
			return false;
		}
	}

	///create lead
	new Ajax.Request('createledid.html',{parameters: 'email=' + _email.value, asynchronous:false, method: 'get', onSuccess: handleLedID});

	if (_ledid == 0) {
		return false;
	}

	var _form = document.getElementById(_formname);
	if(_form != null && typeof _form != "undefined" ){
		_form.submit( );
	}

	return true;
}

/*function for download casino selected by radio buttons*/
function SelectCasino(_casinos, leadid, rd_name){
	var _options = document.getElementsByName(rd_name);
	if(_options != null && typeof _options != "undefined"){
		for(var i=0; i<_options.length; i++){
			if(_options[i].checked){
				DownloadCasino(_casinos[i], leadid);
				return true;
			}
		}
	}
	else{
		return false;
	}
}

/*function for open casino selected by radio buttons*/
function SelectOpenCasino(_casinos, leadid, rd_name){
	var _options = document.getElementsByName(rd_name);
	if(_options != null && typeof _options != "undefined"){
		for(var i=0; i<_options.length; i++){
			if(_options[i].checked){
				OpenCasino(_casinos[i], leadid);
				return true;
			}
		}
	}
	else{
		return false;
	}
}

function SubmitPassLead(){
	var _email = document.getElementById('email');
	if(_email==null || typeof _email=="undefined"){
	    _email = document.getElementById('emailField');
	    if(_email==null || typeof _email=="undefined"){
			return false;
	    }
	}
	///mandatory
	if(!MandatoryCheck(_email)){
		alert(err_messages_stack['mandatory']);
		return false;
	}
	///email validity
	if(!EmailCheck(_email)){
		alert(err_messages_stack['email_validity']);
        return false;
	}
	///terms status
	var _terms = document.getElementById('terms');
	if(_terms!=null && typeof _terms!="undefined"){
		if(!TermsCheck(_terms)){
			alert(err_messages_stack['terms']);
			return false;
		}
	}
	///create lead
	new Ajax.Request('/createledid.html',{parameters: 'email=' + _email.value, asynchronous:false, method: 'get', onSuccess: handleLedID});

	if (_ledid == 0) {
		return false;
	}
	return true;
}
var form_id = '';
function SubmitEcbSnippet( _form_id )
{
	var _email = document.getElementById('email');
	ecb_form = document.getElementById( _form_id );
	form_id = _form_id;
	if(_email==null || typeof _email=="undefined"){
	    _email = document.getElementById('emailField');
	    if(_email==null || typeof _email=="undefined"){
			return false;
	    }
	}
	///mandatory
	if(!MandatoryCheck(_email)){
		alert(err_messages_stack['mandatory']);
		return false;
	}
	///email validity
	if(!EmailCheck(_email)){
		alert(err_messages_stack['email_validity']);
        return false;
	}
	///terms status
	var _terms = document.getElementById('terms');
	if(_terms!=null && typeof _terms!="undefined"){
		if(!TermsCheck(_terms)){
			alert(err_messages_stack['terms']);
			return false;
		}
	}

	///create lead
	new Ajax.Request('/createledid.html',{parameters: 'email=' + _email.value, asynchronous:false, method: 'get', onSuccess: handleLedID});

	if (_ledid == 0) {
		return false;
	}

	if( ecb_form.action.indexOf('?') != -1 )
	{
		ecb_form.action = ecb_form.action + '&lid=' + _ledid;
	}
	else
	{
		ecb_form.action = ecb_form.action + '?lid=' + _ledid;
	}
	setTimeout('submit_form( form_id )', 2000 );

	return false;
}

function submit_form( _form_id )
{
	var _form = document.getElementById( _form_id );
	if(_form != null && typeof _form != "undefined" ){
		_form.submit( );
	}
}

/*submit to download installer for pages that contains "with EPC" checkbox*/
function SubmitDownloadInstaller(_installer){
	var _email = document.getElementById('email');
	if(_email==null || typeof _email=="undefined"){
	    _email = document.getElementById('emailField');
	    if(_email==null || typeof _email=="undefined"){
			return false;
	    }
	}
	///mandatory
	if(!MandatoryCheck(_email)){
		alert(err_messages_stack['mandatory']);
		return false;
	}
	///email validity
	if(!EmailCheck(_email)){
		alert(err_messages_stack['email_validity']);
        return false;
	}
	///terms status
	var _terms = document.getElementById('terms');
	if(_terms!=null && typeof _terms!="undefined"){
		if(!TermsCheck(_terms)){
			alert(err_messages_stack['terms']);
			return false;
		}
	}
	///create lead
	new Ajax.Request('createledid.html',{parameters: 'email=' + _email.value, asynchronous:false, method: 'get', onSuccess: handleLedID});
	///start download process via "hitsyndication" host
	if (_ledid != 0) {
		var _withepc = document.getElementById('withepc');
		if(_withepc == null || typeof _withepc == "undefined")
		{
			_withepc = document.getElementsByName('withepc');
			if(_withepc.length==0){
				DownloadInstaller(_installer);
				return true;
		    }
			else{
				_withepc = _withepc[0];
			}
		}
		if (_withepc.checked) {
			DownloadInstaller(_installer);
		}
	}
	else
		return false;

	return true;
}

/*submit to download casino for second tier pages */
function SubmitDownloadCasino2Tier(_casino,leadid){
	var _email = document.getElementById('email');
	if(_email==null || typeof _email=="undefined"){
	    _email = document.getElementById('emailField');
	    if(_email==null || typeof _email=="undefined"){
			return false;
	    }
	}
	DownloadCasino(_casino,leadid);
	return true;

	///mandatory
	/*if(MandatoryCheck(_email)){
		///email validity
		if(!EmailCheck(_email)){
			if(leadid!= null && trim(leadid) != ''){
				DownloadCasino(_casino,leadid);
			}
	        return true;
		}
		else{
			DownloadCasino(_casino,leadid);
			return true;
		}
	}
	return true;*/
}

/*trim functions*/
/*left trim*/
function ltrim(_string){
	return _string.replace(/^\s+/,"");
}

/*right trim*/
function rtrim(_string){
	return _string.replace(/\s+$/,"");
}
/*trim all*/
function trim(_string){
	return _string.replace(/^\s+|\s+$/g,"");
}

/*display US states*/
function DisplayStates( _selected, _state_elm ){
	var _state = document.getElementById('stateDD');
	if(_state != null && typeof _state != "undefined"){
		_state.disabled = !(_selected.options[_selected.selectedIndex].value == 'United States');
	}
}

////////////////////////////////and new way


/*
 * Collection of function for Refer-A-Friend feature
 */
////////
function _add_friends_fields( ) {
  _tmpl = document.getElementById('template');
  _new_fields = _tmpl.cloneNode(true);
  _new_fields.style.display = 'block';
  _place_holder = document.getElementById('insertHere');
  _place_holder.parentNode.insertBefore(_new_fields, _place_holder);
  document.getElementById('actionbox').style.display = 'block';
  _inputs = _new_fields.getElementsByTagName('INPUT');
  for (var i=0; i< _inputs.length; i++) {
  	if (_inputs[i].type == 'text') {
		_inputs[i].value='';
		_inputs[i].style.backgroundColor='#ffffff';
	}
  }
}

function _refer_fields_mandatory( ) {
  reg = new RegExp(/^([\w]+)(.[\w]+)*@([\w-]+\.){1,5}([A-Za-z]){2,4}$/);
  _inputs = document.getElementById('ref_fields').getElementsByTagName('INPUT');
  _mandatory_err = false;
  _email_err = false;

  for(var i=0;i<4;i++){
  	if(_inputs[i].type == 'text'){
      if(ltrim(_inputs[i].value)==''){
        _mandatory_err=true;
        _inputs[i].style.backgroundColor='#ffff00';
      }
      else if(_inputs[i].id.indexOf('email') != -1 && reg.exec(_inputs[i].value)==null){
        _email_err=true;
        _inputs[i].style.backgroundColor='#ffff00';
      }
    }
  }

  if(_inputs.length > 4){
  	 for (var i = 4; i < _inputs.length; i++) {
	 	if (_inputs[i].type == 'text') {
	 		if (_inputs[i].name == 'frnameField[]') {
				_inputs[i].value = ltrim(_inputs[i].value);
				_inputs[i+1].value = ltrim(_inputs[i+1].value);
	 			if ((_inputs[i].value != '' && _inputs[i+1].value == '') || (_inputs[i].value == '' && _inputs[i+1].value != '')) {
	 				_mandatory_err = true;
	 				_inputs[i].style.backgroundColor = _inputs[i].value == '' ? '#ffff00' : 'ffffff';
	 				_inputs[i+1].style.backgroundColor = _inputs[i+1].value == '' ? '#ffff00' : 'ffffff';
	 			}
	 			else if (_inputs[i].value != '' && reg.exec(_inputs[i+1].value) == null) {
 					_email_err = true;
 					_inputs[i+1].style.backgroundColor = '#ffff00';
 				}
 				else {
 					_inputs[i+1].style.backgroundColor = '#ffffff';
 				}
	 		}
	 	}
	 }
  }

  if(_mandatory_err)
  {
    alert('Please enter the required details into all the highlighted fields.');
	window.location.href="#";
    return false;
  }
  else if(_email_err)
  {
    alert('Please enter a valid email address.');
	window.location.href="#";
    return false;
  }
  return true;
}

function _inputcolor(_el){
  _el.style.backgroundColor = '#ffffff';
}
////////

/*checking phone number*/
function PhoneCheck(_elm){
	reg = new RegExp(_phone_format);
	return reg.exec(_elm.value) != null;
}

////bonus pages
function getBonusCode( ){
	_phone = document.getElementById('phone_number');
	if(!MandatoryCheck(_phone) || _phone.value.replace(_region_code, '').length==0){
		alert(err_messages_stack['phone_mandatory']);
		return false;
	}

	if(!PhoneCheck(_phone)){
		alert(err_messages_stack['phone_validity']);
		return false;
	}

	new Ajax.Request('getbonus.html',{parameters: $('bonus_frm').serialize( ), asynchronous:false, method: 'post', onSuccess: handleBonusCodes});
	return true;
}

function IDTDownload(_casino, leadid){
	_phone = document.getElementById('phone_number');

	if(!MandatoryCheck(_phone) || _phone.value.replace(_region_code, '').length==0){
		if(!confirm(err_messages_stack['phone_mandatory_idt'])){
			return false;
		}
	}
	else{
		if(!PhoneCheck(_phone)){
			if(!confirm(err_messages_stack['phone_validity_idt'])){
				return false;
			}
		}
		else
		{
			_bonuscode = document.getElementById('bonuscode');
			if(_bonuscode != null && typeof _bonuscode != "undefined"){
				if(document.getElementById('bonuscode').value == ''){
					new Ajax.Request('getbonus.html',{parameters: $('bonus_frm').serialize( ), asynchronous:false, method: 'post', onSuccess: handleBonusCodes});
				}
			}
			else{
				_casinos = document.getElementsByName('casino_codes');
				if(_casinos != null && typeof _casinos != "undefined"){
					for(i=0; i<_casinos.length; i++){
						if(_casinos[i].value == ''){
							new Ajax.Request('getbonus.html',{parameters: $('bonus_frm').serialize( ), asynchronous:false, method: 'post', onSuccess: handleBonusCodes});
							break;
						}
					}
				}
			}
		}
	}

	if(_casino.length==0){
		_downloads = document.getElementsByName('download_options');
		if(_downloads != null && typeof _downloads != "undefined"){
			for(i=0; i<_downloads.length; i++){
				if(_downloads[i].checked){
					_casino = _downloads[i].value;
				}
			}
		}
	}

	DownloadCasino(_casino, leadid);
	return true;
}

function selectDownloadIDT(_option){
	$(_option).checked = true;
}

handleBonusCodes = function(t){
	//$('phone_number').readOnly = true;
	_bonus_code = document.getElementById('bonuscode');

	if(_bonus_code != null && typeof _bonus_code != "undefined"){
		_bonus_code.value = t.responseText;
	}
	else
	{
		eval("var _codes = " + t.responseText);
		for(_codefield in _codes ){
			$(_codefield).value = _codes[_codefield];
		}
	}
}

function showTooltipIDT(_el, _text){
	_tooltip = document.getElementById('tooltip');

	if(_tooltip!= null && typeof _tooltip != "undefined"){
		_tooltip.style.visibility = "visible";
	}
	else{
		_tooltip = document.createElement('DIV');
		_tooltip.id = "tooltip";
		_tooltip.className = "tooltipbox";
		_tooltip.style.top = (findPosY(_el)-3) + "px";
		_tooltip.style.left = (findPosX(_el)+_el.clientWidth-5) + "px";
		_tooltip.style.visibility = "visible";
		_tooltip.innerHTML = _text;
		document.body.appendChild(_tooltip);
	}
}

function hideToolTipIDT( ){
	document.getElementById('tooltip').style.visibility = "hidden";
}

///

// EPC download trigger
function dld_submit( ) {
  window.location="E-PlayersCard Setup.exe";
}

/*exit page script - exit traffic*/
function exit_confirmation( _redirect ) {
	if (confirm('Leave ' + document.location.hostname + '?')){
		document.location = _redirect;
	}
}

/*hide box*/
function HideDivBox(_boxname){
	var _box = document.getElementById(_boxname);
	if(_box != null && typeof _box != "undefined"){
		_box.style.display = 'none';
	}
}

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

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