$(document).ready(function() {
		
			apMessageBox.errorImage = "image/error-32x32.png";
			apMessageBox.informationImage = "image/information-32x32.png";
});
function validateEmail(email)
{
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}

function trim(str)
{
  return RTrim(LTrim(str));
}

 
function LTrim(str)
{
  var whitespace = new String(" \t\n\r");

  var s = new String(str);

  if (whitespace.indexOf(s.charAt(0)) != -1) {
    // We have a string with leading blank(s)...

    var j=0, i = s.length;

    // Iterate from the far left of string until we
    // don't have any more whitespace...
    while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
    j++;


    // Get the substring from the first non-whitespace
    // character to the end of the string...
    s = s.substring(j, i);
  }

  return s;
}
function RTrim(str)
{
  // We don't want to trip JUST spaces, but also tabs,
  // line feeds, etc.  Add anything else you want to
  // "trim" here in Whitespace
  var whitespace = new String(" \t\n\r");

  var s = new String(str);

  if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
    // We have a string with trailing blank(s)...

    var i = s.length - 1;       // Get length of string

    // Iterate from the far right of string until we
    // don't have any more whitespace...
    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
      i--;


    // Get the substring from the front of the string to
    // where the last non-whitespace character is...
    s = s.substring(0, i+1);
  }

  return s;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
function succesfn(frm,afflevel){
	if(trim(frm.os0.value)!=''){
		$.ajax({
			  type: "POST",
			  url: 'validateredemption.php',
			  data: "afflevel="+ afflevel +"&promo="+ frm.os0.value, 
			  async: false,	
				beforeSend: function(){
					document.getElementById("ajaxouterdiv").style.display='block';
					document.getElementById("ajaxloader").style.display='block';
				},
			   
				success: function(data) {
					if(trim(data)!=''){
						document.getElementById('ajaxloader').style.display='none';
						document.getElementById('ajaxouterdiv').style.display='none';
						frm.hosted_button_id.value=data;
						frm.submit();
					}
					
			  }
			});
	}
		
}

function validatecondosonly(){
	var frm=document.frmcondosonly;
	var bolstatus=false;var msg='';
	
	if(frm.emailco.value!='' && frm.emailco.value=='Enter a Valid Email Address')
	{
		msg += "Email Address must be entered. \n";
		bolstatus=true;
	}else{
		if(!validateEmail(frm.emailco.value))
		{
			msg += "Invalid Email Address. \n";
			bolstatus=true;
		}
	}
	if(bolstatus==true)
	{
			apMessageBox.error({
			title: "Error!",
			message: msg
		});
	}else{
		succesfn(frm,'P0');
	}
}

function validateaffiliate(){
	var frm=document.frmaffliate;
	var bolstatus=false;var msg='';
	
	if(frm.emailaf.value!='' && frm.emailaf.value=='Enter a Valid Email Address')
	{
		msg += "Email Address must be entered. \n";
		bolstatus=true;
	}else{
		if(!validateEmail(frm.emailaf.value))
		{
			msg += "Invalid Email Address. \n";
			bolstatus=true;
		}
	}
	if(bolstatus==true)
	{
			apMessageBox.error({
			title: "Error!",
			message: msg
		});
	}else{
		succesfn(frm,'P1');
	}
}

function validatevested(){
	var frm=document.frmvested;
	var bolstatus=false;var msg='';
	
		
	if(frm.emailaf.value!='' && frm.emailaf.value=='Enter a Valid Email Address')
	{
		msg += "Email Address must be entered. \n";
		bolstatus=true;
	}else{
		if(!validateEmail(frm.emailaf.value))
		{
			msg += "Invalid Email Address. \n";
			bolstatus=true;
		}
	}
	if(bolstatus==true)
	{
			apMessageBox.error({
			title: "Error!",
			message: msg
		});
	}else{
		succesfn(frm,'P2');
	}
}

function validatefullyvested(){
	var frm=document.frmfullyvested;
	var bolstatus=false;var msg='';
	
	
	if(frm.emailaf.value!='' && frm.emailaf.value=='Enter a Valid Email Address')
	{
		msg += "Email Address must be entered. \n";
		bolstatus=true;
	}else{
		if(!validateEmail(frm.emailaf.value))
		{
			msg += "Invalid Email Address. \n";
			bolstatus=true;
		}
	}
	if(bolstatus==true)
	{
			apMessageBox.error({
			title: "Error!",
			message: msg
		});
	}else{
		succesfn(frm,'P3');
	}
}
