/* Validation Script

*  Aurther: Thamina

*/

var txtid='test';

function xmlhttpPost(strURL, strContent) {

    var xmlHttpReq = false;

    var self = this;

    // Mozilla/Safari

    if (window.XMLHttpRequest) {

        self.xmlHttpReq = new XMLHttpRequest();

    }

    // IE

    else if (window.ActiveXObject) {

        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");

    }

    self.xmlHttpReq.open('POST', strURL, true);

    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    self.xmlHttpReq.onreadystatechange = function() {

        if (self.xmlHttpReq.readyState == 4) {

            updatepage(self.xmlHttpReq.responseText);

        }

    }

    self.xmlHttpReq.send(strContent);

}



function go(which,url)

{

   txtid = which.name;

   xmlhttpPost(url,'w='+which.value);

}

function updatepage(str){

   if(str != "")

   {

      document.getElementById(txtid).style.backgroundColor = "#FF0000";

      document.getElementById(txtid).value = "";

	  alert(str);

   }

   else

   {

	    document.getElementById(txtid).style.backgroundColor = "#FFFFFF";

   }

}

function Validate()

{

var msg = "";

msg += checkname();

msg += checkemail();

msg += checkphone();

msg += checkusername();

msg += checkpass();



if(msg != "" )

 {

    alert(msg);

	return false;

 }

 return true;

}



function checkpass()

{

  var str="";

  if (document.getElementById("password").value  == "")

  {

     document.getElementById("password").style.backgroundColor = "#FF0000";

     str="Please enter the password. \n";

   }

   else

   {

	   if (document.getElementById("password").value.length < 6 || document.getElementById("password").value.length > 32)

	     {

			 document.getElementById("password").style.backgroundColor = "#FF0000";

			 str="Please enter the correct password. \n";

		 }

		else

		{

			 document.getElementById("password").style.backgroundColor = "#FFFFFF";

		}

   }

   if (document.getElementById("password").value  != document.getElementById("conpassword").value)

   {

	    document.getElementById("password").style.backgroundColor = "#FF0000";

		document.getElementById("conpassword").style.backgroundColor = "#FF0000";

	   str="Please enter the correct password and confirmpassword. \n";

   }

   return str;

}



function checkusername()

{

  var str="";

  if (document.getElementById("username").value  == "")

  {

     document.getElementById("username").style.backgroundColor = "#FF0000";

     str="Please enter the username. \n";

   }

   else

   {

	   if (document.getElementById("username").value.length < 6 || document.getElementById("username").value.length > 32)

	     {

			 document.getElementById("username").style.backgroundColor = "#FF0000";

			  str="Please enter the correct username. \n";

		 }

		 else

		{

			 document.getElementById("username").style.backgroundColor = "#FFFFFF";

		}

   }

   return str;

}

function checkname()

{ 

  var str="";

  if (document.getElementById("fname").value  == "")

  {

     document.getElementById("fname").style.backgroundColor = "#FF0000";

     str="Please enter the First Name. \n";

  }

  else

	{

		 document.getElementById("fname").style.backgroundColor = "#FFFFFF";

	}

  if (document.getElementById("lname").value  == "")

  {

     document.getElementById("lname").style.backgroundColor = "#FF0000";

     str="Please enter the Last Name. \n";

  }

  else

	{

		 document.getElementById("lname").style.backgroundColor = "#FFFFFF";

	}

  if (document.getElementById("add1").value  == "")

  {

     document.getElementById("add1").style.backgroundColor = "#FF0000";

     str="Please enter the Address Line. \n";

   }

  else

	{

		 document.getElementById("add1").style.backgroundColor = "#FFFFFF";

	}  

  if (document.getElementById("postcode").value  == "")

  {

     document.getElementById("postcode").style.backgroundColor = "#FF0000";

     str="Please enter the Post Code. \n";

   }

   else

	{

		 document.getElementById("postcode").style.backgroundColor = "#FFFFFF";

	}

  return str;

}



function checkemail()

{

	email = document.getElementById("email").value;

	AtPos = email.indexOf("@");

	StopPos = email.lastIndexOf(".");

	if (email == "") {

	    document.getElementById("email").style.backgroundColor = "#FF0000";

		return "Not a valid Email address" + "\n";

	}

	if (AtPos == -1 || StopPos == -1) {

        document.getElementById("email").style.backgroundColor = "#FF0000";

		return "Not a valid email address" + "\n";

	}

	if (StopPos < AtPos) {

	    document.getElementById("email").style.backgroundColor = "#FF0000";

		return "Not a valid email address" + "\n";

	}

	if (StopPos - AtPos == 1) {

	    document.getElementById("email").style.backgroundColor = "#FF0000";

		return "Not a valid email address" + "\n";

	} 

	return "";

}

function checkphone()

{

 var value = document.getElementById("phone").value;

 if (value  == "")

  {

     document.getElementById("phone").style.backgroundColor = "#FF0000";

     return "Please enter the phone number. \n";

   }

   else

	{

		document.getElementById("phone").style.backgroundColor = "#FFFFFF";

	}

  if(/\D/.test(value))

  {

    document.getElementById("phone").style.backgroundColor = "#FF0000";

    return "Incorrect phone number entered.";

  }

  else

	{

		document.getElementById("phone").style.backgroundColor = "#FFFFFF";

	}

 return "";

}


function byId(el) {
	return document.getElementById(el);
}
function alertID(el) {
	//var element = byId(el);
	alert(byId(el).value);
}


function is_ticked(ele, eleName) {
	
	
	
	/*if (byId(ele).value.length  == 0) {
	
		 byId(ele).style.backgroundColor = "#FF0000";
	
		 alert(eleName);
		 return false;
	
	  }*/
	  if(byId(ele).checked) {
		  return true;
	  } else {
		  alert(eleName);
		  return false;
	  }
	
}
