function $(id)
{
    return document.getElementById(id);
}

function IsEmailValid(checkThisEmail)
{
	var myEMailIsValid = true;
	var myAtSymbolAt = checkThisEmail.indexOf('@');
	var myLastDotAt = checkThisEmail.lastIndexOf('.');
	var mySpaceAt = checkThisEmail.indexOf(' ');
	var myLength = checkThisEmail.length;
	
	if (myAtSymbolAt < 1 )
	 {myEMailIsValid = false}
	
	if (myLastDotAt < myAtSymbolAt)
	 {myEMailIsValid = false}
	
	if (myLength - myLastDotAt <= 2)
	 {myEMailIsValid = false}
	
	if (mySpaceAt != -1)
	 {myEMailIsValid = false}
	
	return myEMailIsValid
}

function webmasters_register() {
	TheForm = document.frm_register;
	
	if (TheForm.contact_name.value == '')
	{
		alert("נא להזין שם מלא של איש הקשר");
		TheForm.contact_name.focus()
		return;
	}
	if (TheForm.url.value == '' || TheForm.url.value == 'http://')
	{
		alert("נא להזין את כתובת אתר האינטרנט");
		TheForm.url.focus()
		return;
	}
	if (TheForm.category_id.value == '0')
	{
		alert("נא לבחור קטגוריה לאתר האינטרנט שברשותכם");
		TheForm.category_id.focus()
		return;
	}
	if (TheForm.phone.value == '')
	{
		alert("נא להזין מספר טלפון להתקשרות");
		TheForm.phone.focus()
		return;
	}
	if (TheForm.email.value == '')
	{
		alert("נא להזין כתובת דואר אלקטרוני");
		TheForm.email.focus()
		return;
	}
	if (TheForm.username.value == '')
	{
		alert("נא לבחור שם משתמש");
		TheForm.username.focus()
		return;
	}
	if (TheForm.password.value == '')
	{
		alert("נא לבחור סיסמא");
		TheForm.password.focus()
		return;
	}
	if (TheForm.password.value != TheForm.password1.value)
	{
		alert("שדה אימות הסיסמא לא תואם לסיסמא שבחרת");
		TheForm.password1.focus()
		return;
	}
	if (!TheForm.terms.checked)
	{
		alert("יש להסכים לתנאי הסכם ההצטרפות אם ברצונך להצטרף למערכת");
		TheForm.terms.focus()
		return;
	}
	
	TheForm.action = 'index.php?p=webmasters_register&cmd=send';
	TheForm.submit();
}

function promoters_register() {
	TheForm = document.frm_register;
	
	if (TheForm.contact_name.value == '')
	{
		alert("נא להזין שם מלא של איש הקשר");
		TheForm.contact_name.focus()
		return;
	}
	if (TheForm.phone.value == '')
	{
		alert("נא להזין מספר טלפון להתקשרות");
		TheForm.phone.focus()
		return;
	}
	if (TheForm.email.value == '')
	{
		alert("נא להזין כתובת דואר אלקטרוני");
		TheForm.email.focus()
		return;
	}
	if (TheForm.username.value == '')
	{
		alert("נא לבחור שם משתמש");
		TheForm.username.focus()
		return;
	}
	if (TheForm.password.value == '')
	{
		alert("נא לבחור סיסמא");
		TheForm.password.focus()
		return;
	}
	if (TheForm.password.value != TheForm.password1.value)
	{
		alert("שדה אימות הסיסמא לא תואם לסיסמא שבחרת");
		TheForm.password1.focus()
		return;
	}
	if (!TheForm.terms.checked)
	{
		alert("יש להסכים לתנאי הסכם ההצטרפות אם ברצונך להצטרף למערכת");
		TheForm.terms.focus()
		return;
	}
	
	TheForm.action = 'index.php?p=promoters_register&cmd=send';
	TheForm.submit();
}

function partners_register() {
	TheForm = document.frm_register;
	
	if (TheForm.contact_name.value == '')
	{
		alert("נא להזין שם מלא של איש הקשר");
		TheForm.contact_name.focus()
		return;
	}
	if (TheForm.phone.value == '')
	{
		alert("נא להזין מספר טלפון להתקשרות");
		TheForm.phone.focus()
		return;
	}
	if (TheForm.email.value == '')
	{
		alert("נא להזין כתובת דואר אלקטרוני");
		TheForm.email.focus()
		return;
	}
	if (TheForm.username.value == '')
	{
		alert("נא לבחור שם משתמש");
		TheForm.username.focus()
		return;
	}
	if (TheForm.password.value == '')
	{
		alert("נא לבחור סיסמא");
		TheForm.password.focus()
		return;
	}
	if (TheForm.password.value != TheForm.password1.value)
	{
		alert("שדה אימות הסיסמא לא תואם לסיסמא שבחרת");
		TheForm.password1.focus()
		return;
	}
	if (!TheForm.terms.checked)
	{
		alert("יש להסכים לתנאי הסכם ההצטרפות אם ברצונך להצטרף למערכת");
		TheForm.terms.focus()
		return;
	}
	
	TheForm.action = 'index.php?p=partners_register&cmd=send';
	TheForm.submit();
}

function order() {
	TheForm = document.frm_register;
	
	if (TheForm.contact_name.value == '')
	{
		alert("נא להזין שם מלא של איש הקשר");
		TheForm.contact_name.focus()
		return;
	}
	if (TheForm.phone.value == '')
	{
		alert("נא להזין מספר טלפון להתקשרות");
		TheForm.phone.focus()
		return;
	}
	if (TheForm.product.value == '')
	{
		alert("נא פרט את השירות/מוצר אותו אתה מציע");
		TheForm.product.focus()
		return;
	}
	
	TheForm.action = 'index.php?p=order&cmd=send';
	TheForm.submit();
}

function login() {
	TheForm = document.frm_login;
	
	if (TheForm.username.value == '')
	{
		alert("נא להזין שם משתמש");
		TheForm.username.focus()
		return;
	}
	if (TheForm.password.value == '')
	{
		alert("נא להזין סיסמא");
		TheForm.password.focus()
		return;
	}

	if (document.getElementById('webmaster').checked || TheForm.system.value == 'webmaster')
	{
		TheForm.action = 'sites/login.php?cmd=login';
	}
	
	if (document.getElementById('advertiser').checked || TheForm.system.value == 'advertiser')
	{
		TheForm.action = 'http://pay-per-leads.com/qualifier/login.php?cmd=login';
	}

	if (TheForm.system.value == 'partner')
	{
		TheForm.action = 'partners/login.php?cmd=login';
	}

	TheForm.target = '_blank';
	TheForm.submit();
}

function onsubmit_system() {
	TheForm = document.frm_login;

	if (document.frm_login.system.value = 'advertiser') {
		document.frm_login.system.value = 'http://pay-per-leads.com/qualifier/login.php?cmd=login';
	} else {
		document.frm_login.system.value = 'sites/login.php?cmd=login';
	}
	document.frm_login.submit();
}

function change_where() {
	TheForm = document.frm_register;

	if (TheForm.where.value == '')
	{
		document.getElementById('other').style.display = 'block';
	}
	else
	{
		document.getElementById('other').style.display = 'none';
	}
}

function show_answer(id, btn, img) {
	document.getElementById(id).style.display = 'block';
	document.getElementById(btn).style.display = 'none';
	document.getElementById(img).src = 'gfx/answer.jpg';
	document.getElementById(img).style.margin = '3px 0px 30px 0px';
}

function show_img_answer(id, btn, img) {
	if (document.getElementById(img).src == 'http://payperlead.co.il/gfx/answer.jpg' || document.getElementById(img).src == 'http://www.payperlead.co.il/gfx/answer.jpg')
	{
		document.getElementById(id).style.display = 'none';
		document.getElementById(btn).style.display = 'block';
		document.getElementById(img).src = 'gfx/question.jpg';
		document.getElementById(img).style.margin = '3px 0px 5px 0px';
	}
	else
	{
		document.getElementById(id).style.display = 'block';
		document.getElementById(btn).style.display = 'none';
		document.getElementById(img).src = 'gfx/answer.jpg';
		document.getElementById(img).style.margin = '3px 0px 30px 0px';
	}
}

function send_a_friend()
{
	TheForm = document.contact;
	
	if (TheForm.name.value == '')
	{
		alert("אנא מלאו את שמכם");
		TheForm.name.focus()
		return;
	}
	if (!IsEmailValid(TheForm.email.value))
	{
		alert("אנא מלאו את כתובת האימייל שלכם");
		TheForm.email.focus()
		return;
	}
	if (TheForm.friend_name.value == '')
	{
		alert("אנא מלאו את שם חברכם");
		TheForm.friend_name.focus()
		return;
	}
	if (!IsEmailValid(TheForm.friend_email.value))
	{
		alert("אנא מלאו את כתובת האימייל של חברכם");
		TheForm.friend_email.focus()
		return;
	}
	
	TheForm.action = "index.php?p=tell_a_friend&cmd=send";
	TheForm.submit();
}

function jump_website() {
	switch (document.getElementById('language').value) {
		case "he":
			url = 'http://payperlead.co.il';
			break;
		case "en":
			url = 'http://pay-per-leads.com';
			break;
		case "fr":
			url = 'http://payperlead.fr';
			break;
	}

	location = url;
}

// --- Language select box --- //

var langBoxOpen = false;
var eKiller = false;

function showLangBox()
{
    if (langBoxOpen)
    {
        $('lang_box').style.display = "none";
        langBoxOpen = false;
    }
    else
    {
        $('lang_box').style.display = "block";
        langBoxOpen = true;
    }
    eKiller = true;
}

function clickedOutsideElement(elemId, evt) 
{
    var theElem = '';
    if(window.event)
    theElem = getEventTarget(window.event);
    else theElem = getEventTarget(evt);

    while(theElem != null) 
    {
        if(theElem.id == elemId)
        return false;

        theElem = theElem.offsetParent;
    }
    return true;
}

function getEventTarget(evt) 
{
    var targ = (evt.target) ? evt.target : evt.srcElement;

    if(targ != null) 
    {
        if(targ.nodeType == 3)
        targ = targ.parentNode;
    }

    return targ;
}

document.onclick = function(evt) 
{
    if(langBoxOpen &! eKiller)
    {
        if(clickedOutsideElement('lang_box', evt))
        {
            $('lang_box').style.display = "none";
            langBoxOpen = false;
        }
        //else
        //alert('Inside the element!');
    }
    if (eKiller) eKiller = false;
}
