var res=true;
function checkAll(form){
	for (i = 0; i < document.form.elements.length; ++i){
		o = document.form.elements[i];
		if (o.type == 'checkbox'){
			o.checked = res;
		}
	}
	res = !res;
	
}

function showLink(form){
	var newWindow = window.open("","", "height=500,width=500");
	
	for (i = 0; i < document.form.elements.length; ++i){
		o = document.form.elements[i];
		if (o.type == 'checkbox' && o.checked==true){
			if (o.name == "che") continue;
			newWindow.document.writeln("http://xfilemirror.com/index.php?c=myuploader&m=search&link=" + o.value);	
		}
	}
	newWindow.focus( );
	
}

function showLoad(){
	a = document.getElementById("showload");
	b = document.getElementById("submitbutton");
	
	a.style.display = "inline";
	b.disabled = true;
}
function checkContactForm(frm){
		if (frm.name.value == "" ){
			changeStyle(frm.name);
			frm.name.focus();
			alert("Please enter your name!");
			return false;
		}
		else if (frm.email.value == ""){
			changeStyle(frm.email);
			frm.email.focus();
			alert("Please enter your email address!");
			return false;
		}
		else if (!checkEmail(frm.email.value)){
			changeStyle(frm.email);
			frm.email.focus();
			return false;
		}
		else if (frm.subject.value == "" ){
			changeStyle(frm.subject);
			frm.subject.focus();
			alert("Please enter subject!");
			return false;
		}
		else if (frm.message.value == "" ){
			changeStyle(frm.message);
			frm.message.focus();
			alert("Please enter message!");
			return false;
		}
		
		return true;
}

function check(){
	var frm = document.fileupload;
	var file = false;
	
	var arrFile 		= 	document.getElementsByName('file[]');
	for (i = 0; i < arrFile.length; ++i) {
		if (arrFile[i].value==''){
			alert("You must choose a file!");
			return false;
		}
	}
	
	var arrHost 		= 	document.getElementsByName('hosting[]');
	var arrCondition	= 	document.getElementsByName('condition[]');
	
	var output = "";
	
	for (i = 0; i < arrHost.length; ++i){
		if (arrHost[i].checked && !arrCondition[i].checked){
			alert("You must accept the conditions!");
			return false;
		}
	}
	
	var hostOne = false;
	for (i = 0; i < arrHost.length; ++i){
		if (arrHost[i].checked){
			hostOne = true;
			break;
		}
	}
	
	if (!hostOne){
		alert("You must select at least one server!");
		return false;
	}
	
	showLoad();
	frm.submit();
	return true;
}

function showTos(loc){
	newWindow = window.open(loc,"", "height=500,width=500");
	newWindow.focus( );
}

function changeStyle(id){
	id.style.height 		   = "18px";
	id.style.width  		   = "352px";
	id.style.backgroundColor   = "#DC8686";
	id.style.color 		   	   = "white";
}

function restoreStyle(id){
	id.style.backgroundColor 	= "white";
	id.style.color 		   		= "black";
	id.style.border 		    = "1px  #7f9db9 solid";
	id.style.width  		    = "352px";
	id.style.height 		    = "18px";
}

function checkContact(){
		var frm = document.fsignup;
		
		restoreStyle(frm.username);
		restoreStyle(frm.realname);
		restoreStyle(frm.email);
		restoreStyle(frm.password);
		restoreStyle(frm.password2);
		
		if (frm.username.value == "" ){
			changeStyle(frm.username);
			frm.username.focus();
			alert("Please enter username!");
			return false;
		}
		else if (frm.realname.value == "" ){
			changeStyle(frm.realname);
			frm.realname.focus();
			alert("Please enter your real name!");
			return false;
		}
		else if (frm.email.value == ""){
			changeStyle(frm.email);
			frm.email.focus();
			alert("Please enter your email address!");
			return false;
		}
		else if (!checkEmail(frm.email.value)){
			changeStyle(frm.email);
			frm.email.focus();
			return false;
		}
		else if (frm.password.value == ""){
			changeStyle(frm.password);
			frm.password.focus();
			alert("Please enter password");
			return false;
		}
		else if (frm.password2.value == ""){
			changeStyle(frm.password2);
			frm.password2.focus();
			alert("Please enter password again");
			return false;
		}
		else if (frm.password.value != frm.password2.value){
			changeStyle(frm.password);
			changeStyle(frm.password2);
			frm.password2.focus();
			alert("Password & Re-type Password doesn't matched!");
			return false;
		}
		else if (!frm.tos.checked){
			frm.tos.focus();
			alert("Please accept the terms of service!");
			return false;
		}
		return true;
}

function checkEmail(str) {
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){;
		   alert("Invalid E-mail ID");
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID");
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID");
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID");
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID");
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID");
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID");
		    return false;
		 }

 		 return true;	
}

function checkLogin(){
	var frm = document.flogin;
	
	restoreStyle(frm.username);
	restoreStyle(frm.password);
	
	if (frm.username.value == ""){
		changeStyle(frm.username);
		frm.username.focus();
		alert("Enter username!");
		return false;
	}
	else if (frm.password.value == ""){
		changeStyle(frm.password);
		frm.password.focus();
		alert("Enter password!");
		return false;
	}
	return true;
}

function checkUsername(){
	var frm = document.fforget;
	if (frm.username.value == ""){
			changeStyle(frm.username);
		frm.username.focus();
		alert("Enter username!");
		return false;
	}
	return true;
}