var afk="";
var isapifil = "0";
var popup="1";

var usagcSiteDomain = 'www.usagc.org';

var languagesArr = ['eng','rom','pol','ind','bul','ger','fin','dut','tur','fra'
					,'heb','rus','ita','deu','jap','tha','chi','prt','man','spn','arb','hun','jpn','swe'];
					
var availableLangsArr = ['arb','bul','chi','deu','dut','eng','fin','fra','ger','heb','hun','ind','ita',
					'jap','jpn','pol','prt','rom','rus','spn','swe','tha','tur'];

var loc = document.location.href;	

if (loc.indexOf("test.") > 0)
{
	usagcSiteDomain = 'server2/usagc';
}
else if (loc.indexOf("testlocal.") > 0 || loc.indexOf("localhost") > 0)
{
	usagcSiteDomain = 'testlocal.usagc.org/usagc';
}
	

function getDefaultAff()
{	
			var defaultAff = "usagc";
			
			var loc = document.location.href;	
			if (loc.indexOf("passport88.") >= 0)
				defaultAff = "passport88com";
			else if (loc.indexOf("passport55.") >= 0)
				defaultAff = "newpassport55com";
			else if (loc.indexOf("visa66.") >= 0)
				defaultAff = "newvisa66com";
			else if (loc.indexOf("visa55.") >= 0)
				defaultAff = "newvisa55com";
			else if (loc.indexOf("visa33.") >= 0)
				defaultAff = "newvisa33com";
			else if (loc.indexOf("visa22.") >= 0)
				defaultAff = "newvisa22com";
			return defaultAff;
}
		
function isValidLanguage(afk_lang)
{
	return (afk_lang in oc(languagesArr));
}

function isAvailableLanguage(afk_lang)
{
	return (afk_lang in oc(availableLangsArr));
}

function oc(a)
{
	var o = {};
	for(var i=0;i<a.length;i++)
	{
		o[a[i]]='';
	}
	return o;
}


function out(stranica,lang)
{

	var aff="";
	var afk="";

	var sURL = window.document.URL.toString();

	if(sURL.indexOf("?") > 0)
	{
		var arrParams = sURL.split("?");
		var arrURLParams = arrParams[1].split("&");
		var arrParamNames = new Array(arrURLParams.length);
		var arrParamValues = new Array(arrURLParams.length);
					
		var i = 0;
		for (i=0;i<arrURLParams.length;i++)
		{
			var sParam =  arrURLParams[i].split("=");
			arrParamNames[i] = sParam[0];
			if (sParam[1] != "")
				arrParamValues[i] = unescape(sParam[1]);
			else
				arrParamValues[i] = "No Value";
		}
					
		afk = arrParamValues[0];

		
		aff = afk.substr(0,afk.length-3).toString();
		
		
		
	}

	var string = stranica;
	
	if (afk != "") 
		string += "?afk=" + aff+lang;
	window.open(string, target="_top"); 
}



var PresedState="";
var State;
var activeBT;
function ChangeImg(N_Img,State){
	var myImg = N_Img;
	//if(activeBT != 	myImg){
	activeBT = 	myImg;
	var N_Img=document.getElementById(N_Img);	
	//alert(N_Img)			
	if(N_Img!=null){
		if (State==1){				
			N_Img.src=N_Img.src.replace("_off","_on");
			} else {											
			N_Img.src=N_Img.src.replace("_on","_off");
		}		
	}
	//}			
}


function setColor(el, bg) {
  if (el.style) el.style.backgroundColor = bg;
}

function echeck(str)
{
	//var filter = /^[a-zA-Z0-9]+[\._a-zA-Z0-9\-]*[a-zA-Z0-9]@[a-zA-Z0-9\-]+[\._a-zA-Z0-9\-]*[a-zA-Z0-9]\.[a-zA-Z]{2,4}$/;
	var filter  = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	
	if (!filter.test(str))
	{
		return false;
	}
	else
	{
		return true;
	}			
}

function alertModal(msg)
{
			msg = msg.replace(/<\/?[a-z][a-z0-9]*[^<>]*>/g, " \n ");
			alert(msg);

	
}	



function agreeOut()
{
	IsAgree = true;
}

		
function topSelfFrame(afk_name)
{
	if(isTopFrameAffiliate(afk_name))
	{
		if (self != top) top.location = self.location;
		if (top != self) top.location.replace('http://' + usagcSiteDomain + '/step1.aspx?'+getURLParameters());	
	}
}

function getURLParameters() 
{
	var sURL = window.document.URL.toString();
	
	if (sURL.indexOf("?") > 0)
	{
		var arrParams = sURL.split("?");
		var arrURLParams = arrParams[1].split("&");
		var arrParamNames = new Array(arrURLParams.length);
		var arrParamValues = new Array(arrURLParams.length);
		
		var i = 0;
		for (i=0;i<arrURLParams.length;i++)
		{
			var sParam =  arrURLParams[i].split("=");
			arrParamNames[i] = sParam[0];
			if (sParam[1] != "")
				arrParamValues[i] = unescape(sParam[1]);
			else
				arrParamValues[i] = "No Value";
		}
		
		return arrParamNames[0]+"="+arrParamValues[0];
	}
	else
	{
		return "";
	}
}
function onUnload() 
{
	
	if (IsAgree==true) return true ;
	var sURL = window.document.URL.toString();
	if (sURL.indexOf("pop=1") > 0)
	{
			var url = 'http://greencardforall.org/?afk=gc4allstep1land';
			var popupWin = window.open(url);
			if (popupWin) popupWin.focus();		
	}
	else
	{
		if (sURL.indexOf("adsmarketnewcampaigns01") > 0)
		{
			var url = 'http://greencardforall.org/?afk=gc4allstep1land';
			var popupWin = window.open(url);
			if (popupWin) popupWin.focus();
		}	

	}
}

			
function setCountryAreaCode(countryName,countryCodeObj)
{		         
	countryCodeObj.value = countryName.split('~')[1];
	
	countryCodeObj.style.color="#00cc00";
}
	         
function CountryAreaCheck(str)
{
	var filter = /^\d{0,3}$/;
	
	if (!filter.test(str))
	{
		return false;
	}
	else
	{
		return true;
	}			
}	

function TelephoneAreaCheck(str)
{
	var filter = /^\d{0,4}$/;
	if (!filter.test(str))
	{
		return false;
	}
	else
	{
		return true;
	}			
}

function TelephoneNumberCheck(str)
{
	var filter = /^\d{7,20}$/;
	if (!filter.test(str))
	{
		return false;
	}
	else
	{
		return true;
	}			
}


function _uVoid() { return; }
function trace(cid)
{
    var img=new Image(1,1);
    img.src='http://bravocomm.com/tvmstats/tvm.gif?l=' + window.location +'&cid=' + cid;
    img.onload=function() { _uVoid(); }
}
   
		function fInit()
		{ 	

                
		if (afk_name.toLowerCase() == "adoopmintlnov" || afk_name=="gcClENGL")
				{
				isapifil="1";								
				}	
			if ((afk_name == "XteCLCA") || (afk_name == "XteCLIE") || (afk_name == "XteCLIE") || (afk_name == "XteCLDK") || (afk_name == "XteCLZA"))
			{
				document.getElementById('pixel2').innerText  = '<img src="http://ad.yieldmanager.com/pixel?id=139794&t=2" width="1" height="1" />';
				document.getElementById('pixel2').style.display = 'none';			
			}
			
			if (afk_name == 'luccmintl')
			{
				//<!-- Advertiser 'JediMedia Group ',  Include user in segment 'Pop Impression Counter' - DO NOT MODIFY THIS PIXEL IN ANY WAY -->
				document.getElementById('pixel1').innerText  = '<img src="http://ad.yieldmanager.com/pixel?id=124199&t=2" width="1" height="1" />';
				//<!-- End of segment tag --> 
				document.getElementById('pixel1').style.display = 'none';
			}
			
			if (afk_name == 'CehClengcallba' && document.getElementById('cehText'))
			{
			    document.getElementById('cehText').style.display = 'inline';
			}
			
			if ((loc.indexOf("passport55.") >= 0) || (afk_name == 'newpasport55com'))
			{
				trace('00b00bcf-3ca4-46b1-8f88-98bdaf6d3764');
			}
			
			if ((loc.indexOf("passport22.") >= 0)  || (afk_name == 'passport22com'))
			{			
				trace('5661ebb4-e089-4984-a08d-9327afa60318');
			}
						
						if ((loc.indexOf("visa33.") >= 0)  || (afk_name == 'newvisa33com'))
			{			
				trace('503543c8-862b-4613-ba15-3a36507dfa6b');
			}
					
			
			topSelfFrame(afk_name);
			var c = 'Select Country';
			document.getElementById('cCountry').options[0]= new Option(c,"-1");
			start();
			document.getElementById('cCountry').options[0].selected = true;
			
			
			if (document.getElementById('cMarital').selectedIndex == 0)
			{
				document.getElementById('SposeTR1').style.display = 'none';
				document.getElementById('SposeTR2').style.display = 'none';
			}
			else
			{
				document.getElementById('SposeTR1').style.display = 'inline';
				document.getElementById('SposeTR2').style.display = 'inline';
			}
			
			try
			{ 
				document.getElementById('cFname').focus();
			}
			catch(e)
			{
			}
			
		}
		function start()
		{	
			var qs = new Querystring();
			var afk = qs.get("afk",defaultAff);
			var af_lang = "";
			var aff = "";
			
			if(afk.length > 2) 
			{
				af_lang = afk.substr(afk.length-3,afk.length).toString();
			}
			
			if (isValidLanguage(af_lang))		
				aff = afk.substr(0,afk.length-3).toString();
			else
				aff = afk;

			//document.getElementById('framereport').src = 'AjaxCalls/GetLog.aspx?page=1&affiname='+aff+'&lang='+af_lang_id;
			Log(aff,af_lang_id);
		}
	
		function fGo()
		{
			if (checkInput(document.getElementById("Form1")))
			{
				document.getElementById("op").value = 1;
				Form1.action = "http://" + usagcSiteDomain + "/step1.aspx?isapi="+isapifil+"&"+ getURLParameters();
				Form1.submit();
			}
		}
		
		function openspouse(index)
		{
			if (index == 1){
				document.getElementById("SposeTR1").style.display = 'inline';
				document.getElementById("SposeTR2").style.display = 'inline';
			} else {
				document.getElementById("SposeTR1").style.display = 'none';
				document.getElementById("SposeTR2").style.display = 'none';
			}
		}
		var answ=0;
		function checkInput(form) 
		{
			
			var form = document.getElementById(form.id);
			
			var valid = true;
			var err = "\n";//			
			if (form.cFname.value == "") {
				valid = false;
				document.getElementById("tdFname").style.color="#dd0000";
				setColor(form.cFname, bgBad);				
				form.cFname.focus();
			} else {
				document.getElementById("tdFname").style.color="#142061";
				setColor(form.cFname, bgGood);
			}
			if (form.cLname.value == "") {
				valid = false;
				document.getElementById("tdLname").style.color="#dd0000";
				setColor(form.cLname, bgBad);
			} else {
				document.getElementById("tdLname").style.color="#142061";
				setColor(form.cLname, bgGood);
			} 
			var MailOK = true;
			if ((echeck(form.cEmail.value) == false) || (form.cEmail.value.length < 8)) {
				MailOK = false;
				valid = false;
				document.getElementById("tdEmail").style.color="#dd0000";
				document.getElementById("tdEmail2").style.color="#dd0000";
				setColor(form.cEmail, bgBad);
				setColor(form.cEmail2, bgBad);
				//err = err + "mail1 not good"
				err = err + " - Please insert a valid e-mail\n";
			} else {
				document.getElementById("tdEmail").style.color="#142061";
				document.getElementById("tdEmail2").style.color="#142061";
				setColor(form.cEmail, bgGood);
				setColor(form.cEmail2, bgGood);
			}
			
			if (form.cEmail.value!=form.cEmail2.value) {
				valid = false;
				document.getElementById("tdEmail").style.color="#dd0000";
				document.getElementById("tdEmail2").style.color="#dd0000";
				setColor(form.cEmail, bgBad);
				setColor(form.cEmail2, bgBad);
				err = err + " - You typed two different e-mail addresses. Please type the same email on both fields.\n";
			} else {
				if(MailOK != false){
				document.getElementById("tdEmail").style.color="#142061";
				document.getElementById("tdEmail2").style.color="#142061";
				setColor(form.cEmail, bgGood);
				setColor(form.cEmail2, bgGood);
				}
			}
			if (MailOK && valid)
				if (answ!=0)
				{
					//alert(answ);
					valid = false;
					document.getElementById("tdEmail").style.color="#dd0000";
					setColor(form.cEmail, bgBad);
					document.getElementById("tdEmail2").style.color="#dd0000";
					setColor(form.cEmail2, bgBad);
					err = err + " - Email does not exist.\n";
				} 
				else  
				{
					document.getElementById("tdEmail").style.color="#142061";
					setColor(form.cEmail, bgGood);
					document.getElementById("tdEmail2").style.color="#142061";
					setColor(form.cEmail2, bgGood);
				}	

			if (form.cCountry.value == "-1") {
				valid = false;
				document.getElementById("tdCountry").style.color="#dd0000";
				setColor(form.cCountry, bgBad);
				err = err + " - You forgot to select your country of birth!\n";
			} else {
				document.getElementById("tdCountry").style.color="#142061";
				setColor(form.cCountry, bgGood);
			}	

			var phone = document.getElementById("cCountryArea").value + document.getElementById("cTelephoneArea").value + document.getElementById("cTelephoneNumber").value;
			var phoneMobile = document.getElementById("cCountryAreaMobile").value + document.getElementById("cTelephoneAreaMobile").value + document.getElementById("cTelephoneNumberMobile").value;

			if ((TelephoneNumberCheck(phone) == false) && (TelephoneNumberCheck(phoneMobile) == false)) {
				valid = false;
				setColor(document.getElementById("cCountryArea"), bgBad);
				setColor(document.getElementById("cTelephoneArea"), bgBad);
				setColor(document.getElementById("cTelephoneNumber"), bgBad);
				document.getElementById("tdCountryTelephone").style.color="#dd0000";
				
				setColor(document.getElementById("cCountryAreaMobile"), bgBad);
				setColor(document.getElementById("cTelephoneAreaMobile"), bgBad);
				setColor(document.getElementById("cTelephoneNumberMobile"), bgBad);
				err = err + " - Enter your phone number !";
			} else {
				setColor(document.getElementById("cCountryArea"), bgGood);
				setColor(document.getElementById("cTelephoneArea"), bgGood);
				document.getElementById("tdCountryTelephone").style.color="#142061";
				setColor(document.getElementById("cTelephoneNumber"), bgGood);
				
				setColor(document.getElementById("cCountryAreaMobile"), bgGood);
				setColor(document.getElementById("cTelephoneAreaMobile"), bgGood);
				document.getElementById("tdCountryTelephoneMobile").style.color="#142061";
				setColor(document.getElementById("cTelephoneNumberMobile"), bgGood);
			}
			if (!valid) alert(" - You didn't fill form correctly, please check the RED fields!\n" + err)
			return valid;
		}

		function send_email(mail,mail2)
		{		
			if (false && answ==0)
			{
			/*
				var m = mail.split("@");
				if (m[1]!=null)
				{
					var n = m[1].split(".");
					if (n[1]!=null) 
						{
						var a=n[0];
						var b=n[1];
						if (a.length<2) m="a";
						if (b.length<2) m="a";
						}
					else {
						m="a";
					}
				
				}
				else
				{
				m="a";
				}	
				
			*/	
				if ((mail!="") && (mail==mail2) && (echeck(mail)))
				{
						var xmlHttp;
						xmlHttp=GetXmlHttpObject();
						if (xmlHttp!=null)
						{
							xmlHttp.onreadystatechange=function()  
							{ 
								if(xmlHttp.readyState == '4')  
								{		
									if(xmlHttp.status == '200')  
									{ 
										//answ=xmlHttp.responseText;
									} 
								} 
							};							
							xmlHttp.open('GET','insert.aspx?email='+mail,true);
							xmlHttp.send(null);
						}		
				}
				else
				{
					var str ="";
					if ((mail.length<1)|(mail2.length<1)) str=str +" - You must enter your email (MUST BE BETWEEN 8 and 40 SYMBOLS!!!)\n";
					if (mail!=mail2) str=str +" - Please write your e-mail again to ensure you wrote your e-mail correctly\n";
					//if (m=="a") str=str +" - Please insert a valid e-mail\n";
					alert(str);
				}
			
			}
		}
			
		function check_email(mail)
		{
			if ((mail!="") && (echeck(mail)))
				createXMLHttpRequest(mail.split("@")[1]);
		}
			
		function createXMLHttpRequest(domain)  
		{      
			var xmlHttp;    
			xmlHttp=GetXmlHttpObject();
			if (xmlHttp!=null)
			{
				xmlHttp.onreadystatechange = function()  
				{ 
					if(xmlHttp.readyState == '4')  
					{		
						if(xmlHttp.status == '200')  
						{ 
							answ=xmlHttp.responseText;
						} 
					} 
				};
				xmlHttp.open("GET", 'ajaxcalls/emailcheck.aspx?domain='+domain, false);
				xmlHttp.send(null);
			}		
		} 
			
          
        function open_acc()
		{
			var ema = document.getElementById("cEmail").value;
			ema = ema.replace(' ','');

			var ema2 = document.getElementById("cEmail2").value;
			ema2 = ema2.replace(' ','');
			
			if ((document.getElementById("cFname").value != "") &&(document.getElementById("cLname").value != "")&& (echeck(ema) != false) )
			createXMLHttpRequestAcc(document.getElementById("cFname").value,document.getElementById("cLname").value,ema,document.getElementById("cCountry").value);	
		}       
		
		function createXMLHttpRequestAcc(Fname,Lname,emaill,Country)  
		{   //IE       
			var xmlHttp;
			xmlHttp=GetXmlHttpObject();
			if (xmlHttp!=null)
			{
				xmlHttp.onreadystatechange = function()  
				{ 
					if(xmlHttp.readyState == '4')  
					{		
						if(xmlHttp.status == '200')  
						{ 						
							if (document.getElementById("htmlsessid").value == '' && xmlHttp.responseText != '')
								document.getElementById("htmlsessid").value = xmlHttp.responseText;	
						} 
					} 
				};
				xmlHttp.open("GET", 'ajaxcalls/newaccount.aspx?Fname='+Fname+'&Lname='+Lname+'&emaill='+emaill+'&Country='+Country+'&afk='+afk, true); 
				xmlHttp.send(null); 
			}
		} 		
		
		function Log(aff,af_lang_id)  
		{   //IE       
			var xmlHttp;
			xmlHttp=GetXmlHttpObject();
			if (xmlHttp!=null)
			{
				xmlHttp.onreadystatechange = function()  
				{ 
					if(xmlHttp.readyState == '4')  
					{		
						if(xmlHttp.status == '200')  
						{ 	
							if (document.getElementById("htmlsessid").value == '' && xmlHttp.responseText != '')
								document.getElementById("htmlsessid").value = xmlHttp.responseText;
						} 
					} 
				};
				
				xmlHttp.open("GET", 'AjaxCalls/RemoteLog.aspx?page=1&affiname='+aff+'&lang='+af_lang_id, true); 
				xmlHttp.send(null); 
			}
		} 		
		
function Querystring()
{

	var querystring=location.search.substring(1,location.search.length);

	var args = querystring.split('&');


	for (var i=0;i<args.length;i++)
	{
		var pair = args[i].split('=');

		// Fix broken unescaping
		temp = unescape(pair[0]).split('+');
		name = temp.join(' ');

		temp = unescape(pair[1]).split('+');
		value = temp.join(' ');

		this[name]=value;
	}

	this.get=Querystring_get;
}

function Querystring_get(strKey,strDefault)
{

	var value=this[strKey];
	if (value==null)
	{
		value=strDefault;
	}
	return value;
}


function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
	// Firefox, Opera 8.0+, Safari
	xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
	// Internet Explorer
	try
		{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	catch (e)
		{
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

var bgBad = "#FFE2B5";
var bgGood = "white";


// JS Error Handler
function errorHandler(message, url, line)
{   
    
    //-------------------------------------------------------------------
    // Log the Error
    //-------------------------------------------------------------------
    
    var xmlHttpRequest;
    // a variable to hold our instance of an XMLHTTP object.

    var bHookTheEventHandler = false;
    // used to switch on/off the handling
    // of any response from the server.

    // Checking if IE-specific document.all collection exists 
    // to see if we are running in IE 
    if (document.all)
    { 
        xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
    } else { 
        xmlHttpRequest = new XMLHttpRequest(); 
    }
    

    //----------------------------------------------------------------        
    // Roll the error information into an xml string:
    //----------------------------------------------------------------
	var errorStr = "line=" + escape(line) + "&url=" + escape(url) + "&message=" + escape(message);

    //------------------------------------------------------------
    // If you want to see some sort of response to prove (on the 
    // client end) that something has happened,
    // set bHookTheEventHandler = true 
    // otherwise false is probably all we want.
    //------------------------------------------------------------
    if(bHookTheEventHandler)
    {
        xmlHttpRequest.onreadystatechange = function()  
							{ 
								if(xmlHttp.readyState == '4')  
								{		
									if(xmlHttp.status == '200')  
									{ 									
									} 
								} 
							};						
    }

    xmlHttpRequest.open("POST", "AjaxCalls/LogError.aspx?" + errorStr, bHookTheEventHandler);

    //finally send the call
    xmlHttpRequest.send( null );  
    
    //------------------------------------------------------------
    // Finished Logging the Error
    //------------------------------------------------------------
                    
    return false;
}

window.onerror = errorHandler;
