var xmlHttp

// SEND REQUEST
function gethint(str){ 
	if (str.length > 0){ 
		var url='checkuname.php?&u=' + str
		xmlHttp=GetXmlHttpObject(stateChanged)
		xmlHttp.open('GET', url , true)
		xmlHttp.send(null)
	}else{ 
		document.getElementById('username_check').innerHTML=""
	} 
} 

// PAGE UPDATE
function stateChanged(){ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=='complete'){ 
		document.getElementById('username_check').innerHTML=xmlHttp.responseText 
	} 
}

//---VALIDATE THE PROJECT FORM
function check_project_form(){
	error = 0;

	if (document.getElementById("yesexisting").checked == true){
		passwordstatus = 0;
		username_wrong = 0;
	}
	if (document.getElementById("noexisting").checked == true){
		usernamestatus = 0;
		password_wrong = 0;
	}
	
	//Email Must Be Valid
	emailvalue = document.project_details.email_id.value
	emailregex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i ;
	
	if (!emailvalue.match(emailregex)){
		if (emailstatus == 1){
			error = 1;
			document.getElementById("email_id").style.border = "1px solid red";
			document.getElementById("email_id").style.background=red;
			document.getElementById("email_check").style.display = "inline";
			document.getElementById("email_check").innerHTML = "<img alt='' src='images/warning.png' style='margin-top:6px;width:20px;height:20px;float:left;' /><p style='margin-left:3px;float:left;margin-top:11px;color:red;font-size:11px'>Please Enter A Valid Email Address</p>";
		}
	}else{
		if (emailstatus == 1){
			document.project_details.email_id.style.border = "1px solid green";
			document.project_details.email_id.style.background=green;
			document.getElementById("email_check").innerHTML = "<img alt='' src='images/tick2.png' style='margin-top:6px;width:20px;height:20px;float:left;' />";
		}
	}
	
	//the password is at least 6 characters
	password = document.project_details.new_password.value
	passregex = /^.{6,}$/i ;
	
	if (!password.match(passregex)){
		if (passwordstatus == 1){
			error = 1;
			document.project_details.new_password.style.border = "1px solid red";
			document.project_details.new_password.style.background=red;
			document.getElementById("password_check").innerHTML = "<img alt='' src='images/warning.png' style='margin-top:3px;width:20px;height:20px;float:left;' /><p style='margin-left:3px;float:left;margin-top:8px;color:red;font-size:11px'>The Password Must Be At Least 6 Characters</p>";
		}
	}else{
		if (passwordstatus == 1){
			document.project_details.new_password.style.border = "1px solid green";
			document.project_details.new_password.style.background=green;
			document.getElementById("password_check").innerHTML = "<img alt='' src='images/tick2.png' style='margin-top:3px;width:20px;height:20px;float:left;' />";
		}
	}
		
	//confirm the password
	confirm_password = document.project_details.confirmpassword.value

	if ((password != confirm_password) || (confirm_password =='')){
		if (passwordstatus != 0){
			error = 1;
			document.project_details.confirmpassword.style.border = "1px solid red";
			document.project_details.confirmpassword.style.background=red;
			document.getElementById("password_confirm_check").innerHTML = "<img alt='' src='images/warning.png' style='margin-top:3px;width:20px;height:20px;float:left;' /><p style='margin-left:3px;float:left;margin-top:8px;color:red;font-size:11px'>Passwords Do Not Match</p>";
		}
	}else{
		if (passwordstatus != 0){
			document.project_details.confirmpassword.style.border = "1px solid green";
			document.project_details.confirmpassword.style.background=green;
			document.getElementById("password_confirm_check").innerHTML = "<img alt='' src='images/tick2.png' style='margin-top:3px;width:20px;height:20px;float:left;' />";
		}
 
	}
	
	//Must Include A Name
	namevalue = document.project_details.existing_username.value
	nameregex = /^.{1,20}$/i;
	//nameregex = /^[a-z0-9A-Z]([- a-z0-9A-Z])*$/i ;
	
	if (!namevalue.match(nameregex)){
		if (usernamestatus != 0){
			error = 1;
			document.project_details.existing_username.style.border = "1px solid red";
			document.project_details.existing_username.style.background=red;
			document.getElementById("existing_username_check").innerHTML = "<img alt='' src='images/warning.png' style='margin-top:2px;width:20px;height:20px;float:left;' /><p style='margin-left:3px;float:left;margin-top:7px;color:red;font-size:11px'>Please Enter A Valid Username</p>";
		}
	}else{
		if (usernamestatus != 0){
			document.project_details.existing_username.style.border = "1px solid green";
			document.project_details.existing_username.style.background=green;
			document.getElementById("existing_username_check").innerHTML = "<img alt='' src='images/tick2.png' style='margin-top:3px;width:20px;height:20px;float:left;' />";
		}
	}
}

function check_captcha(){
	//Capcha Must Be Correct
	acapresult = document.project_details.capresult.value
	cqst = document.project_details.qst.value;
	caps = new Array('6','5','2','8','4');
	if (acapresult != caps[cqst] ){
		document.project_details.capresult.style.border = "1px solid red";
		document.getElementById("capresulterror").style.display = "inline";
		document.getElementById("capresultcheck").style.display = "none";
		error = 1;
	}else{
		document.project_details.capresult.style.border = "1px solid green";
		document.getElementById("capresulterror").style.display = "none";
		document.getElementById("capresultcheck").style.display = "inline";
	}
}

/* function check_password(pass,username){
	var myurl='check_existing_username.php?pass='+pass+'&username='+username+' ';
	document.getElementById("existing_password_check").innerHTML = myurl;
} */

//create the HMLHTTPRequest object
function getXMLHTTPRequest(){
	try{
		req = new XMLHttpRequest();
	}catch(err1){
		try{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (err2){
			try{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(err3){
				req = false;
			}
		}
	}
	return req;
}
var http = getXMLHTTPRequest();

//construct our server request,define a callback function and send the request to the server.
function check_password(pass,username){
	var myurl='check_existing_username.php?pass='+pass+'&username='+username+'';
	myRand = parseInt(Math.random()*99999999999999999999);
	//add random number to URL to avoid cache problems
	var modurl = myurl+"&rand="+myRand;
	http.open("GET", modurl, true);
	//set up the callback function
	http.onreadystatechange = useHttpResponse;
	http.send(null);
}

//the callback function
function useHttpResponse(){
	password_wrong = 0;
	existing_customer_password = 1;
	if (http.readyState == 4){
		if (http.status == 200){
			// var response = http.responseText;
			// document.getElementById('old_password_check').innerHTML = response;
 
			var root = http.responseXML.getElementsByTagName("pass")[0];
			if (root.getElementsByTagName("border")[0].childNodes[0].nodeValue == 0 ){
				$border_style = "1px solid red";
			}else{
				$border_style = "1px solid green";
			}
			document.project_details.existing_password.style.border = $border_style;
			document.project_details.existing_password.style.background = root.getElementsByTagName("background")[0].childNodes[0].nodeValue;
			message = root.getElementsByTagName("message")[0].childNodes[0].nodeValue;
			image = root.getElementsByTagName("image")[0].childNodes[0].nodeValue;
			password_wrong = root.getElementsByTagName("error")[0].childNodes[0].nodeValue;

			var display_message = "<img alt='' src='"+image+"' style='margin-top:2px;width:20px;height:20px;float:left;' /><p style='margin-left:7px;width:275px;margin-top:0px;color:red;font-size:11px;line-height:1;'>" +message+ "</p>";
			if ( password_wrong == 1 ){
				document.project_details.existing_username.style.border = "1px solid red";
				document.project_details.existing_username.style.background="#FFF1EF";
				document.getElementById("existing_username_check").innerHTML = "";
			}else{
				usernamestatus = 1;
				check_project_form();
			}
			document.getElementById('existing_password_check').innerHTML = display_message;
		}
	}else{
		document.getElementById('existing_password_check').innerHTML = '<img src="images/loading_icon.gif" style="width:20px;height:20px;">';
	}
}
//-------------------------------------------------------------------------------------------------------------------------------------------
var http2 = getXMLHTTPRequest();

function check_username(username){
	var myurl='check_username.php?username='+username+'';
	myRand = parseInt(Math.random()*99999999999999999999);
	//add random number to URL to avoid cache problems
	var modurl = myurl+"&rand="+myRand;
	http2.open("GET", modurl, true);
	//set up the callback function
	http2.onreadystatechange = useHttpResponse2;
	http2.send(null);
}

function useHttpResponse2(){
	username_wrong = 0;
	new_customer_username = 1;
	if (http2.readyState == 4){
		if (http2.status == 200){
			var root = http2.responseXML.getElementsByTagName("usercheck")[0];
			if (root.getElementsByTagName("border")[0].childNodes[0].nodeValue == 0 ){
				$border_style = "1px solid red";
			}else{
				$border_style = "1px solid green";
			}
			document.project_details.new_username.style.border = $border_style;
			document.project_details.new_username.style.background = root.getElementsByTagName("background")[0].childNodes[0].nodeValue;
			message = root.getElementsByTagName("message")[0].childNodes[0].nodeValue;
			image = root.getElementsByTagName("image")[0].childNodes[0].nodeValue;
			username_wrong = root.getElementsByTagName("error")[0].childNodes[0].nodeValue;

			var display_message2 = "<img alt='' src='"+image+"' style='margin-top:2px;width:20px;height:20px;float:left;' /><p style='margin-left:4px;width:245px;float:left;margin-top:8px;color:red;font-size:11px;line-height:1;'>" +message+ "</p>";
			document.getElementById('username_check').innerHTML = display_message2;
		}
	}else{
		document.getElementById('username_check').innerHTML = '<img src="images/loading_icon.gif" style="width:17px;height:17px;">';
	}
}

//---------------------------------------------------------------------------------------------------------------------------------------------------------------
var z = 0;
function addfile(){

	z = z+1;
	var newfile = document.createElement('INPUT');
	newfile.setAttribute ('type','file');
	newfile.setAttribute ('name','project_attach'+z+'');
	newfile.setAttribute ('onclick','document.project_details.attach_status.options[0].selected = true');

	var newfiled = document.createElement('INPUT');
	newfiled.setAttribute ('type','text');
	newfiled.setAttribute ('name','project_attach_d'+z+'');
	newfiled.setAttribute ('value','Description');

	document.getElementById('add').appendChild(newfile);
	document.getElementById('add').appendChild(newfiled);
}

 

function setStyle(x){
	document.getElementById(x).style.background="#F5F4F3";
}


//------POP UP BOX ---------------------
var help_box = false;
var help_boxShadow = false;
var help_box_shadow_size = 4;
var help_boxMaxWidth = 200;
var help_boxMinWidth = 100;
var help_box_iframe = false;
var help_box_is_msie = (navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('opera')==-1 && document.all)?true:false;
	
function show_help_box(e,box_content){
		
	var bodyWidth = Math.max(document.body.clientWidth,document.documentElement.clientWidth) - 20;
	
	if(!help_box){
		help_box = document.createElement('DIV');
		help_box.id = 'help_box';
		help_boxShadow = document.createElement('DIV');
		help_boxShadow.id = 'help_boxShadow';
		
		document.body.appendChild(help_box);
		document.body.appendChild(help_boxShadow);	
		
		if(help_box_is_msie){
			help_box_iframe = document.createElement('IFRAME');
			help_box_iframe.frameborder='5';
			help_box_iframe.style.backgroundColor='#FFFFFF';
			help_box_iframe.src = '#'; 	
			help_box_iframe.style.zIndex = 100;
			help_box_iframe.style.position = 'absolute';
			document.body.appendChild(help_box_iframe);
		}
			
	}
	
	help_box.style.display='block';
	help_boxShadow.style.display='block';
	if(help_box_is_msie)help_box_iframe.style.display='block';
		
	var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
	if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0; 
	var leftPos = e.clientX + 10;
		
	help_box.style.width = null;	// Reset style width if it's set 
	help_box.innerHTML = box_content;
	help_box.style.left = leftPos + 'px';
	help_box.style.top = e.clientY + 10 + st + 'px';
		
	help_boxShadow.style.left =  leftPos + help_box_shadow_size + 'px';
	help_boxShadow.style.top = e.clientY + 10 + st + help_box_shadow_size + 'px';
		
	if(help_box.offsetWidth>help_boxMaxWidth){	/* Exceeding max width of tooltip ? */
		help_box.style.width = help_boxMaxWidth + 'px';
	}
		
	var tooltipWidth = help_box.offsetWidth;		
	if(tooltipWidth<help_boxMinWidth)tooltipWidth = help_boxMinWidth;
	
	help_box.style.width = tooltipWidth + 'px';
	help_boxShadow.style.width = help_box.offsetWidth + 'px';
	help_boxShadow.style.height = help_box.offsetHeight + 'px';		
	
	if((leftPos + tooltipWidth)>bodyWidth){
		help_box.style.left = (help_boxShadow.style.left.replace('px','') - ((leftPos + tooltipWidth)-bodyWidth)) + 'px';
		help_boxShadow.style.left = (help_boxShadow.style.left.replace('px','') - ((leftPos + tooltipWidth)-bodyWidth) + help_box_shadow_size) + 'px';
	}
		
	if(help_box_is_msie){
		help_box_iframe.style.left = help_box.style.left;
		help_box_iframe.style.top = help_box.style.top;
		help_box_iframe.style.width = help_box.offsetWidth + 'px';
		help_box_iframe.style.height = help_box.offsetHeight + 'px';
	}
	
}
	
function hide_help_box(){
	help_box.style.display='none';
	help_boxShadow.style.display='none';		
	if(help_box_is_msie)help_box_iframe.style.display='none';		
}

function dosubmit(customer_status,ignore_login_box){
	if(ignore_login_box == 0){ 
		emailstatus = 1;

		if(customer_status == 'noexisting'){
			passwordstatus = 1;
			if(new_customer_username != 1){
				error = 1;
				document.project_details.new_username.style.border = "1px solid red";
				document.project_details.new_username.style.background="#FFF1EF";
				document.getElementById("username_check").innerHTML = "<img alt='' src='images/warning.png' style='margin-top:2px;width:20px;height:20px;float:left;' /><p style='margin-left:4px;float:left;margin-top:7px;color:red;font-size:11px;line-height:1;'>This Name Is Already Taken</p>";
			}
		}else if (customer_status == 'existing'){
			usernamestatus = 1;
			if(existing_customer_password != 1){
				error = 1;
				document.project_details.existing_password.style.border = "1px solid red";
				document.project_details.existing_password.style.background="#FFF1EF";
				document.getElementById("existing_password_check").innerHTML = "<img alt='' src='images/warning.png' style='margin-top:2px;width:20px;height:20px;float:left;' /><p style='margin-left:4px;float:left;margin-top:0px;color:red;font-size:11px;line-height:1;'>The Username or Password you entered is incorrect <br /> Please try again</p>";
			}
		}
		
		check_project_form();
		check_captcha();
		
		order_title = document.project_details.project_title.value;
		if (order_title ==''){
			error = 1;
			document.project_details.project_title.style.border = "1px solid red";
		}

		//if the error variable has been turned on OR the password that an existing customer used is wrong OR the username that a non existing customer is not valid then return false
		if ((error == 1) || (password_wrong == 1) || (username_wrong == 1)){
			alert ("Oops! We think you missed something - please make sure you have filled correctly all the sections outlined in red");
			return false;
		}else{
			return true;
		}
	}else if(ignore_login_box == 1){
		error = 0;
		order_title = document.project_details.project_title.value;
		if (order_title ==''){
			error = 1;
			document.project_details.project_title.style.border = "1px solid red";
		}
		check_captcha();
		if (error == 1){
			alert ("Oops! We think you missed something - please make sure you have filled correctly all the sections outlined in red");
			return false;
		}else{
			return true;
		}
	}
}

function newwindow()
{
	window.open('terms_2.php','Terms','width=950,height=500,scrollbars=yes,resizable=yes');
} 
