function trim(x)
{
  var ch,c
  ch=x.toString()
  c=ch.charAt(0)
  while (c==" ")
  { ch=ch.slice(1)
  c=ch.charAt(0)
  }
  c=ch.charAt(ch.length-1)
  while(c==" ")
  { ch=ch.slice(0,-1)
  c=ch.charAt(ch.length-1)
  }
  return ch;
}

//===========================================================================
function send(frm, p1, v1, p2, v2, p3, v3, p4, v4, p5, v5)
{ if(!frm) return false;
  if(p1)
  { if (p1=='action') eval('document.'+frm+'.'+p1+'="'+v1+'";');
    else eval('if (document.'+frm+'.'+p1+') document.'+frm+'.'+p1+'.value="'+v1+'";');
  }
  if(p2) eval('if (document.'+frm+'.'+p2+') document.'+frm+'.'+p2+'.value="'+v2+'";');
  if(p3) eval('if (document.'+frm+'.'+p3+') document.'+frm+'.'+p3+'.value="'+v3+'";');
  if(p4) eval('if (document.'+frm+'.'+p4+') document.'+frm+'.'+p4+'.value="'+v4+'";');
  if(p5) eval('if (document.'+frm+'.'+p5+') document.'+frm+'.'+p5+'.value="'+v5+'";');
  eval('document.'+frm+'.submit();');
  return true;
}

//===========================================================================
function check_url(url)
{
  var re = new RegExp("^http[s]?://[\\w\\.-]+\\.[A-Za-z]{2,}.*");
  var re2 = new RegExp("^http[s]?://[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}");
  return(re.test(url) || re2.test(url));
}

//===========================================================================
function check_mail(email)
{ var re = new RegExp("^[\\w\\.-]+@[\\w\\.-]+\\.[A-Za-z]{2,}");
  return (re.test(email));
}

//===========================================================================
function only_number(num) {
 var re = new RegExp("^[\\d]*$");
  return (re.test(num));
}

//===========================================================================
function CheckAllFormData() {
  //If All right then is_ok=1
  is_ok=1;

  tmp1 = document.contact.ch_num;
  if( trim(tmp1.value)!='' && only_number(trim(tmp1.value)) ) { document.getElementById('ch_num_text').style.color=''; }
  else {
	is_ok=0;
	document.getElementById('ch_num_text').style.color='red';
	document.contact.ch_num.focus();
  }
  tmp1 = document.contact.bodymessg;
  if( trim(tmp1.value)!='' ) { document.getElementById('bodymessg_text').style.color=''; }
  else {
	is_ok=0;
	document.getElementById('bodymessg_text').style.color='red';
	document.contact.bodymessg.focus();
  }

  tmp1 = document.contact.email;
  if( check_mail(tmp1.value) ) { document.getElementById('email_text').style.color=''; }
  else {
	is_ok=0;
	document.getElementById('email_text').style.color='red';
	document.contact.email.focus();
  }
  
  tmp1 = document.contact.question;
  if( trim(tmp1.value)!='' ) { document.getElementById('question_text').style.color=''; }
  else {
	is_ok=0;
	document.getElementById('question_text').style.color='red';
	document.contact.question.focus();
  }

  tmp1 = document.contact.fname;
  if( trim(tmp1.value)!='' ) { document.getElementById('fname_text').style.color=''; }
  else {
	is_ok=0;
	document.getElementById('fname_text').style.color='red';
	document.contact.fname.focus();
  }  

  if (is_ok==1) {
	if(confirm('Send now?')) { document.contact.action.value='2'; document.contact.submit(); }
	else return false;
  }
	else return false;
}



// JavaScript Document
self.focus();

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_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

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_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

var img_cache = new Object();

function parse_images() {
	if (document.getElementById) {
		var ar = document.getElementsByTagName('img');
		for( var x = 0; ar[x]; x++ ) {
			var im = ar[x];
			if(im.getAttribute) { 
				im.hoversrc = im.getAttribute('hoversrc');
			}
			if(im.hoversrc) {
				im.rootsrc = im.src;				
				im.onmouseout = function () {
					this.src = this.rootsrc;
				}
				
				if(!img_cache[im.hoversrc]) {
					img_cache[im.hoversrc] = new Image();
					img_cache[im.hoversrc].src = im.hoversrc;
				}

				im.onmouseover = function () {			
					this.src = this.hoversrc;								
				}
			}
		}
	}
}

function doRedirect(sTargetURL)
{
  TargetURL = sTargetURL;
  setTimeout("timedRedirect(TargetURL)", 1*1000 );
}

function timedRedirect(TargetURL)
{
  parent.location.replace(TargetURL);
}


function toggle(id)
{
	if(this.innerText=="More Info")
	 this.innerText="Hide Info";
	 else
	 this.innerText="More Info";
	var obj = document.getElementById(id);
	if(obj.style.display=='block'){
		obj.style.display='none';
	}else{
		obj.style.display='block';
	}
}


// grey out effect
function grayOut(vis, options) {
// Pass true to gray out screen, false to ungray
// options are optional. This is a JSON object with the following (optional) properties
// opacity:0-100 // Lower number = less grayout higher = more of a blackout 
// zindex: # // HTML elements with a higher zindex appear on top of the gray out
// bgcolor: (#xxxxxx) // Standard RGB Hex color code
// grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
// Because options is JSON opacity/zindex/bgcolor are all optional and can appear
// in any order. Pass only the properties you need to set.
var options = options || {}; 
var zindex = options.zindex || 50;
var opacity = options.opacity || 70;
var opaque = (opacity / 100);
var bgcolor = options.bgcolor || '#000000';
var dark=document.getElementById('darkenScreenObject');
if (!dark) {
// The dark layer doesn't exist, it's never been created. So we'll
// create it here and apply some basic styles.
// If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
var tbody = document.getElementsByTagName("body")[0];
var tnode = document.createElement('div'); // Create the layer.
tnode.style.position='absolute'; // Position absolutely
tnode.style.top='0px'; // In the top
tnode.style.left='0px'; // Left corner of the page
tnode.style.overflow='hidden'; // Try to avoid making scroll bars 
tnode.style.display='none'; // Start out Hidden
tnode.id='darkenScreenObject'; // Name it so we can find it later
tbody.appendChild(tnode); // Add it to the web page
dark=document.getElementById('darkenScreenObject'); // Get the object.
}
if (vis) {
// Calculate the page width and height 
if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
var pageWidth = document.body.scrollWidth+'px';
var pageHeight = document.body.scrollHeight+'px';
} else if( document.body.offsetWidth ) {
var pageWidth = document.body.offsetWidth+'px';
var pageHeight = document.body.offsetHeight+'px';
} else {
var pageWidth='100%';
var pageHeight='100%';
} 
//set the shader to cover the entire page and make it visible.
dark.style.opacity=opaque; 
dark.style.MozOpacity=opaque; 
dark.style.filter='alpha(opacity='+opacity+')'; 
dark.style.zIndex=zindex; 
dark.style.backgroundColor=bgcolor; 
dark.style.width= pageWidth;
dark.style.height= pageHeight;
dark.style.display='block'; 
} else {
dark.style.display='none';
}
}

var linkId;
var trackId;
var imgPath = 'http://images.creditcardflyers.com/credit-cards/';
function apply(cardname, cardimage, lnkid, tid) {
	grayOut(true);
	linkId = lnkid;
	trackId = tid;
	if (trackId) trackId += '/';
	setTimeout("window.location.href='http://www.creditcardflyers.com/application/'+trackId+linkId;", 3000);
	//setTimeout("window.location.href='http://www.creditcardflyers.com/application/?linkid='+linkId+'&SubID='+trackId;", 3000);
	var winW = document.body.clientWidth;
	var winH = document.body.clientHeight;

	document.getElementById('div_pleasewait').style.left=(winW/2)-200+'px';
	document.getElementById('div_pleasewait').style.top=100+document.documentElement.scrollTop+'px';
	document.getElementById('wait_card_img').src = imgPath + cardimage;
	document.getElementById('wait_card_name').innerHTML = cardname;
	document.getElementById('lead').innerHTML = '<iframe src="http://www.creditcardflyers.com/application/lead.html" width="0" height="0"></iframe>';
	document.getElementById('div_pleasewait').style.visibility = 'visible';
}
