/*
 * 
 * email: ( char, number, hypehns, dash, dot, @, +, )
 * 
 * 
 */


/**
 * @author xaprio
 */

/*
 * A very simple but powerful javascript validation by Arif.
 */


function validateName(alphane)
{
	if((alphane=="First Name")||(alphane=="Last Name")){
		return false;
	}
	else{
	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==39) || (hh==32) || (hh==45)) {
		  	}
		  	else {
		  		return false;
		  	}
		}
		}
	return true;
}
	function weblink(val){
		var wptrn=/^http[s]?:\/\/(.*)+$/;
		if((!val.match(wptrn))){
			return false;
		}
	}

	function validatePhone(phoneNum){
		if(phoneNum=='') return false;
	for(var j=0; j<phoneNum.length; j++)
		{
		  var alphaa = phoneNum.charAt(j);
		  	var hh = alphaa.charCodeAt(0);
		  	if ((hh==32) || (hh==40) || (hh==41) || (hh==43) || (hh==45) || (hh>47 && hh<58 )) {
		  	}
		  	else {
		  		return false;
		  	}
		}
	}

	function noInteger(txt){
	for(var j=0; j<txt.length; j++)
		{
		  var alphaa = txt.charAt(j);
		  	var hh = alphaa.charCodeAt(0);
		  	if ((hh>47 && hh<58 )) {
				return false;
		  	}
		}
	}
	
	function currencyVal(val){
	for(var j=0; j<val.length; j++)
		{
		  var alphaa = val.charAt(j);
		  	var hh = alphaa.charCodeAt(0);
		  	if ((hh>47 && hh<58 ) || (hh==36) || (hh==44) || (hh==46)) {
				
		  	}
			else{
				return false;
			}
		}
	}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
	
	function validateFile(filename){
		document.getElementById('fake_file_upload').value=filename;
		var ext = filename.substr(filename.lastIndexOf('.')).toLowerCase(); 
		if((ext==".txt") || (ext==".doc") || (ext==".docx") || (ext==".ppt") || (ext==".pptx") || (ext==".pps") || (ext==".ppsx") || (ext==".xls") || (ext==".xlsx") || (ext==".pdf")){
			document.getElementById('proj_doc').setAttribute('tooltipText', 'This is a valid ' + document.getElementById('proj_doc').getAttribute('txt'));
			return true;
		}
		else{
			document.getElementById('fake_file_upload').setAttribute('tooltipText', 'Invalid file extension. You can only upload (doc,docx,pdf,ppt,pps,xls,txt) files. Please upload a valid ' + document.getElementById('fake_file_upload').getAttribute('txt'));
			return false;
		}
	}
	
	function validateResume(filename){
		document.getElementById('fake_file_upload').value=filename;
		var ext = filename.substr(filename.lastIndexOf('.')).toLowerCase(); 
		if((ext==".doc") || (ext==".txt") || (ext==".docx") || (ext==".rtf") || (ext==".pdf")){
		}
		else{
			return false;
		}
	}
	
	function echeck(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   return false;
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false;
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false;
		}
		 if (str.indexOf(at,(lat+1))!=-1){
		    return false;
		 }
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false;
		 }
		 if (str.indexOf(dot,(lat+2))==-1){
		    return false;
		 }
		 if (str.indexOf(" ")!=-1){
		    return false;
		 }
			for(var j=0; j<str.length; j++)
				{
				  var alphaa = str.charAt(j);
				  	var hh = alphaa.charCodeAt(0);
				  	if ((hh>=47 && hh<=58 ) || (hh>=64 && hh<=91 ) || (hh>=97 && hh<=122 ) || (hh==45) || (hh==95 ) || (hh==46 ) || (hh==64 ) || (hh==43 )) {
				  	}
					else{
						return false;
					}
				}
	}

function callme(id,msg){
	document.getElementById(id).setAttribute('tooltipText', msg);
	document.getElementById(id).tooltiptext = msg;
	document.getElementById(id).focus();
	document.getElementById(id).select();
	return false;
}

	function validate(){
		var all=document.getElementsByTagName('input');
		for (var i = 0; i < all.length; i++) {
			if (all[i].type != "file")
			all[i].value = trim(all[i].value);
		  if (all[i].style.display != "none") {
		  	if ((all[i].type != "image") && (all[i].name != "fake_file_upload")){
		  	if (all[i].name == "email") {
				if (echeck(all[i].value) == false) {
					return callme(all[i].id,'Please enter a valid ' + all[i].getAttribute('txt'));
				}
			}
			else {
				if (all[i].name == "phone") {
					if ((validatePhone(all[i].value) == false) && (all[i].value!='')) {
						return callme(all[i].id,'Please enter a valid ' + all[i].getAttribute('txt'));
					}
				}
					else {
						if (all[i].type == "file") {
							if ((validateFile(all[i].value)==false) && (all[i].value!='')) {
								return callme('fake_file_upload','Invalid file extension. You can only upload (doc,docx,pdf,ppt,pps,xls,txt) files. Please upload a valid ' + document.getElementById('fake_file_upload').getAttribute('txt'));
							}
						}
						else {
						if (trim(all[i].value) == "") {
							return callme(all[i].id,'Please enter a valid ' + all[i].getAttribute('txt'));
						}
						else {
							if ((all[i].name == "fname") || (all[i].name == "lname")) {
								if (validateName(all[i].value) == false) {
									if ((all[i].value == "First Name") || (all[i].value == "Last Name")) {
										all[i].value = '';
									}
									return callme(all[i].id,'Invalid ' + all[i].getAttribute('txt') + ': Only Letters, hyphens and apostrophes are allowed');
								}
							}
							else {
								if (all[i].name == "your_web_uri") {
									if (weblink(all[i].value) == false) {
										return callme(all[i].id,'Please enter a valid ' + all[i].getAttribute('txt'));
									}
								}
								else {
									if (all[i].name == "country") {
										if ((noInteger(all[i].value) == false) && (all[i].value) != '') {
											return callme(all[i].id,'Numeric values are not allowed. Please enter a valid ' + all[i].getAttribute('txt'));
										}
									}
									else {
										if (all[i].name == "expected_budget") {
											if (currencyVal(all[i].value) == false) {
												return callme(all[i].id,'Please enter a valid ' + all[i].getAttribute('txt') + ". Only ',', '.' and numeric values are allowed");
											}
										}
									}
								}
							}
						}
					}
				}
					}
				}
		}
		}
		desc = document.getElementById('proj_description');
		desc.value = trim(desc.value);
		if(desc.value==""){
			return callme(desc.id,'Please enter a valid ' + desc.getAttribute('txt'));
		}
	}

	function createProjectType(val){
		if (val == '') {
			document.getElementById("other_proj").style.display = 'inline';
			document.getElementById("other_proj_type").style.display = 'inline';
			document.getElementById("other_proj_type").focus();
			document.getElementById("other_proj_type").select();
		}
		else {
			document.getElementById("other_proj_type").value=''; 
			document.getElementById("other_proj_type").style.display='none'; 
			document.getElementById("other_proj").style.display = 'none';
		}
	}