// Javascipt document

function addbookmark(){
title = "GetCupid.co.uk";
url = "http://www.getcupid.co.uk";
if (window.sidebar) {
// Mozilla Firefox Bookmark
window.sidebar.addPanel(title, url,"");
} else if( window.external ) {
// IE Favorite
window.external.AddFavorite( url, title);
}
else if(window.opera && window.print) {
// Opera Hotlist
return true; } 

}

// Ajax - check if registered username exists
function checkusername(username){
	var xmlhttp=false;
		try {
			xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');  // try xmlHttp=new ActiveXObject("Microsoft.XMLHTTP") which is for Internet Explorer 6.0+
        }catch (e){
			try{
				xmlhttp = new
				ActiveXObject('Microsoft.XMLHTTP');     //try xmlHttp=new ActiveXObject("Microsoft.XMLHTTP") which is for Internet Explorer 5.5+
            	}catch (E){
					xmlhttp = false;
            	}
        	}
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
                xmlhttp = new XMLHttpRequest();         //If we were able to get a working active x object, start an XMLHttpRequest
        }

	xmlhttp.open('GET', 'ajax.username.check.php?username='+username, false);
	xmlhttp.send('');
	if (xmlhttp.status == 200) {
	return xmlhttp.responseText.indexOf('true') == -1;
	} else return false; 
}

function check_admin_login(username, password){
    var xmlhttp=false;
        try {
            xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');  // try xmlHttp=new ActiveXObject("Microsoft.XMLHTTP") which is for Internet Explorer 6.0+
        }catch (e){
            try{
                xmlhttp = new
                ActiveXObject('Microsoft.XMLHTTP');     //try xmlHttp=new ActiveXObject("Microsoft.XMLHTTP") which is for Internet Explorer 5.5+
                }catch (E){
                    xmlhttp = false;
                }
            }
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
                xmlhttp = new XMLHttpRequest();         //If we were able to get a working active x object, start an XMLHttpRequest
        }

    xmlhttp.open('GET', 'ajax.admin_login_check.php?username='+username+'&password='+password, false);
    xmlhttp.send('');
    if (xmlhttp.status == 200) {
    return xmlhttp.responseText.indexOf('true') == -1;
    }else return false;
}


function check_admin(form){

    if (form.username.value == ""){
        alert("You must enter USERNAME."); form.username.focus();
        return (false);
    }

    if (form.password.value == ""){
        alert("You must enter PASSWORD."); form.username.focus();
        return (false);
    }

    if ((check_admin_login(form.username.value, form.password.value))){
        alert("Incorect Usename or Password"); form.username.focus();
        return (false);
    }



}

function check_user_login(username, password,args){
    var xmlhttp=false;
        try {
            xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');  // try xmlHttp=new ActiveXObject("Microsoft.XMLHTTP") which is for Internet Explorer 6.0+
        }catch (e){
            try{
                xmlhttp = new
                ActiveXObject('Microsoft.XMLHTTP');     //try xmlHttp=new ActiveXObject("Microsoft.XMLHTTP") which is for Internet Explorer 5.5+
                }catch (E){
                    xmlhttp = false;
                }
            }
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
                xmlhttp = new XMLHttpRequest();         //If we were able to get a working active x object, start an XMLHttpRequest
        }

    xmlhttp.open('GET', 'ajax.user_login_check.php?username='+username+'&password='+password+'&which='+args, false);
    xmlhttp.send('');
    if (xmlhttp.status == 200) {
    return xmlhttp.responseText.indexOf('true') == -1;
    }else return false;
}

function check_login(form){

    if (form.username.value == ""){
        alert("You must enter USERNAME."); form.username.focus();
        return (false);
    }

    if (form.password.value == ""){
        alert("You must enter PASSWORD."); form.username.focus();
        return (false);
    }

    if ((check_user_login(form.username.value, form.password.value,'first'))){
        alert("Incorect Usename or Password"); form.username.focus();
        return (false);
    }

    if ((check_user_login(form.username.value, form.password.value,'second'))){
        alert("Sorry your account has been for some reason suspended.\n If you still want to log in, please contact us.."); form.username.focus();
        return (false);
    }

}

// Registering form validation
function validate_signup_form(form) {

	if (form.lowage.selectedIndex >= form.highage.selectedIndex){
		alert("From age value must be less then to age value.\n Please fix this problem. "); form.highage.focus();
		return (false);
	}

	if (form.dobday.selectedIndex <= 0){
		alert("Please select DAY of your birth."); form.dobday.focus();
		return (false);
	}

	if (form.dobmonth.selectedIndex <= 0){
		alert("Please select MONTH of your birth."); form.dobmonth.focus();
		return (false);
	}

	if (form.dobyear.selectedIndex <= 0){
		alert("Please select YEAR of your birth."); form.dobyear.focus();
		return (false);
	}

	if (form.username.value == ""){
		alert("You must enter USERNAME."); form.username.focus();
		return (false);
	}

	if (form.username.value.length < 5 || form.username.value.length > 20 ){
		alert("Please enter at least 5, 10 at maximum characters in the USERNAME field."); form.username.focus();
		return (false);
	}

	if ((checkusername(form.username.value))){
		alert("Username exists. Try another one."); form.username.focus();
		return (false);
	}

	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.";
	var checkStr = form.username.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++){
		ch = checkStr.charAt(i);
  	for (j = 0;  j < checkOK.length;  j++)
  		if (ch == checkOK.charAt(j)) break;
  		if (j == checkOK.length){
    	allValid = false;
  		break;
  	}
	}

	if (!allValid){
		alert("Please enter only allowed characters in the \"username\" field.\nLetters,numeric characters,underscore and dot character are the only allowed."); form.username.focus();
		return (false);
	}

	if (form.password.value.length < 5 ){
		alert("Please enter at least 5 characters in the PASSWORD field."); form.password.focus();
		return (false);
	}

	if (form.password.value != form.repassword.value){
		alert("The two passwords are not the same."); form.repassword.focus();
		return (false);
	}

    if (form.postcode.value == ""){
        alert("You must enter your postcode\n Postcode format example : ml3 6yu."); form.username.focus();
        return (false);
    }

	// test if valid email address, must have @ and .
	var checkemail = "@.";
	var checkstr = form.email.value;
	var emailvalid = false;
	var emailat = false;
	var emaildot = false;
	for (i = 0;  i < checkstr.length;  i++){
		ch = checkstr.charAt(i);
		for (j = 0;  j < checkemail.length;  j++){
			if (ch == checkemail.charAt(j) && ch == "@") emailat = true;
			if (ch == checkemail.charAt(j) && ch == ".") emaildot = true;
    		if (emailat && emaildot) break;
    		if (j == checkemail.length)
    		break;
  		}
		// if both the @ and . were in the string
		if (emailat && emaildot){
			emailvalid = true; break;
  		}
		}
		if (!emailvalid){
			alert("Email not valid. Re-enter your email.\nexample : somename@bleyonder.co.uk"); form.email.focus();
			return (false);
		}

    var c=confirm("You have to use real email address.\nIn case you loose passwrod, new one will be sent to this email.\nAs you will agree, please check your email ASAP.\nAs confirmation email will be send to you.\nAre you sure you want to continue ?");
    if (c==false){
        form.email.focus();
        return (false);
    }


	if (!form.over18.checked){
		alert("You have not read and agreed to terms and conditions."); form.over18.focus();
		return (false);
	}
}
