// JavaScript Document
var xmlhttp;
function GetXmlHttpObject(){
	if (window.XMLHttpRequest)  {  
	  return new XMLHttpRequest();// code for IE7+, Firefox, Chrome, Opera, Safari
	}
	if (window.ActiveXObject)  {  // code for IE6, IE5
	  return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function stateChanged() {
	if (xmlhttp.readyState==4) {
		document.getElementById("mid-Main-Box-Contents").innerHTML=xmlhttp.responseText;
	}
}
function stateChangedRegUser() {
	if (xmlhttp.readyState==4) {
		document.getElementById("but-Main-Box-Contents").innerHTML=xmlhttp.responseText;
	}
}
function stateChangedSignUp() {
	if (xmlhttp.readyState==4) {
		document.getElementById("main-Contents").innerHTML=xmlhttp.responseText;
	}
}
function stateChangedUpload() {
	if (xmlhttp.readyState==4) {
		document.getElementById("main-Contents00").innerHTML=xmlhttp.responseText;
	}
}

/*********************** Form Validation ********************************/
function validateEnquiry(theForm) {
var reason = "";

  reason += validateUsername(theForm.name);
  reason += validatePassword(theForm.pwd);
  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.phone);
  reason += validateEmpty(theForm.from);      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  return true;
}
function validateEnquiry(theForm) {
var reason = "";  
  reason += validateEmail(theForm.email);
  reason += validateEmpty(theForm.name);
  reason += validateEmpty(theForm.enquiry);
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  return true;
}
function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 2)) {
        fld.style.background = 'Yellow'; 
        error = "The username is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The password is the wrong length. \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
}  
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    }
    return error;
}
/*********************** Sign up form chaecking ********************************/
function alphanumeric(alphane){
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++)	{	
		  var alphaa = numaric.charAt(j);
		  var hh = alphaa.charCodeAt(0);
		  if((hh > 64 && hh<91) || (hh > 96 && hh<123) || (hh==32) ) {  }		
		  else	{ return false; }
 	}
 	return true;
}
function checkSignupForm(){
	var errorMsg='';
	var fname=document.createAccount.fname.value;
	var gender=document.createAccount.gender.value;
	var dob=document.createAccount.dob.value;
	var country=document.createAccount.country.value;
	var uname=document.createAccount.uname.value;
	var pass=document.createAccount.pass.value;
	var cpass=document.createAccount.cpass.value;
	var mobilenum=document.createAccount.mobilenum.value;
	var phone=document.createAccount.phone.value;
	var model=document.createAccount.model.value;
	var mails=document.createAccount.mails.value;
	var agree=document.createAccount.agree.checked;
	if(!(fname.length>2 && alphanumeric(fname))) errorMsg="Invalid Full Name\n";
	if(gender!=null && gender.length<=1) errorMsg+="Invalid Gender";
	if(dob.length<=7) errorMsg+="Invalid DOB";
	if(country.length<2) errorMsg+="Invalid Country\n";
	if(uname.length<2) errorMsg+="Invalid User Name\n";
	if(pass.length<5) errorMsg+="Invalid Password\n";
	if(pass!=cpass) errorMsg+="Password doesn't match";
	if(mobilenum.length<10) errorMsg+="Invalid Mobile Number\n";
	if(phone.length<2) errorMsg+="Invalid Phone\n";
	if(model.length<1) errorMsg+="Invalid Model\n";
	if(!agree) errorMsg+="You Have To Agree With our Terms and Conditions";
	if(errorMsg.length>1) {
		alert(errorMsg);
		return false;
	}
	return true;	
}
function stateChangedCommon() {
	if (xmlhttp.readyState==4) {
		if(xmlhttp.responseText.length>1){
			if(xmlhttp.responseText=="User"){				
				alert("Username already exists !");
				document.createAccount.uname.focus();
				document.createAccount.uname.value="";
			}
			else if(xmlhttp.responseText=="Mobile"){				
				alert("Mobile number already registered");
				document.createAccount.mobilenum.focus();
				document.createAccount.mobilenum.value="";
			}
		}
	}
}
function check_signupField(str){	
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)	  {
	  alert ("Browser does not support HTTP Request");
	  return;
	}	
	var url="http://localhost/Mobile-Community/signup_validator.php";
	url=url+"?req="+str;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=stateChangedCommon;	
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);	
}
function submitMe(str,user){
	if(str=='SignUp'){
		document.linksform.links.value="SignUp";
		document.linksform.submit();
	}
	else if(str=='Login'){
		document.linksform.links.value="Login";
		document.linksform.submit();
	}
	else if(str=='Logout'){
		document.linksform.links.value="Logout";
		document.linksform.submit();
	}
	else if(str=='profilePage'){
		document.linksform.links.value="profilePage";
		document.linksform.userid.value=user;
		document.linksform.submit();
	}
	else if(str=='accountPage'){
		document.linksform.links.value="accountPage";
		document.linksform.submit();
		//window.location.href='http://localhost/Mobile-Community/logout.php?username='+user;
	}	
	else if(str=='upload'){
		document.linksform.links.value="upload";
		document.linksform.submit();
	}
}

function myAccLinks(str){
	document.myAccLinksForm.acclinks.value=str;
	document.myAccLinksForm.submit();
}

function stateChangedCat() {
	if (xmlhttp.readyState==4) {
		document.getElementById("subcat").innerHTML=xmlhttp.responseText;
	}
}
function showSubcat(str,table){
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)	  {
	  alert ("Browser does not support HTTP Request");
	  return;
	}	
	var url="choose_cat.php";
	url=url+"?req="+str+"&tab="+table;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=stateChangedCat;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);	
}
function sendInfo(id){
	document.details.sn.value=id;
	document.details.submit();
}

function closeBox(id){
	document.getElementById(id).style.display="none";
}
function showImage(image,id){
	document.getElementById(id).src=image;
	document.getElementById(id).border="0";
	document.getElementById(id).style.display="inline";
}
function hideImage(image,id){	
	document.getElementById(id).src="";
	document.getElementById(id).style.display="none";
}
function zoom(id,image){
	document.getElementById(id).src=image;
	document.getElementById(id).border="0";
	document.getElementById(id).style.display="inline";
}

function checkemail()
{	
	str=document.getElementById("txtftmailId").value;
	apos=str.indexOf("@");
    dotpos=str.lastIndexOf(".");
	if(str == "")
	{
		alert("You Must Enter an Email Id !!");
		document.getElementById("txtftmailId").focus;
		return false;
	}
	else if(apos < 1 || ((dotpos-apos) < 2))
	{
		alert("Please Enter a Valid Email Id !!");
		document.getElementById("txtftmailId").focus;
		return false;
	}
	else
	{
		return true;
	}
}
