/*
This file will contain functions for all validations in java script that we need in ZAD Solutions 

----------------------------------------------------
---- Created By:		 		 Lobna Mohamed	----
---- Creation Date: 			 01-06-2008		----
---- Last Modification Date:	 01-06-2008		----
----------------------------------------------------		
*/

//This variable is used to count the number of errors in the forms
var invalid_items=0;

function start()
{
	emptySpans("errorSpan");
	invalid_items=0
}
function emptySpans(class_name)
{
	//this function takes the span of the error message class name and when it's called it empty all the spans of this 
	//class to refill them in the comming validation
	var spans= document.getElementsByTagName("span");
	for (i=0;i<spans.length;i++)
	{
		//reset the error spans 
		if(spans[i].className==class_name)
			spans[i].innerHTML='';
	}//for
	
}

function strip_spaces(mystr) {
	var newstring = "";
	if (mystr.indexOf(' ') != -1) {
		string = mystr.split(' ');
		for (var i=0;i<string.length;i++)
		{
			newstring += string[i];
		}
		return newstring;
	} else { return mystr; }
}

function strip_spaces2(mystr) {
	
	var newstring = "";
	if (mystr.indexOf('&nbsp;') != -1) {
		string = mystr.split('&nbsp;');
		for (var i=0;i<string.length;i++)
		{
			newstring += string[i];
					}
		return newstring;
	} else { return mystr; }
}

/////////////////////////////////////////////////////////////////////////////////
 function strip_enters(mystr) {
	var newstring = "";
	var finalstr="";
	if (mystr.indexOf('\r\n') != -1) {
		string = mystr.split(' ');
		for (var i=0;i<string.length;i++)
		{
			newstring += string[i];
		}
		return newstring;
	} else { return mystr; }
}

function validateUsername(field_id,field_label,msg_span,availabilty_field,min_length,max_length,prevent_special_char,required)
{	
	var messege=''
	var field=document.getElementById(field_id);
	if(!field)	return;
	if(required){
		if(field.value.length==0){
			invalid_items++
			if(messege=='')
			messege="Please enter the "+ field_label	
		}//if empty
		
	}//if required
	if(min_length)
	{
		if(field.value.length<min_length){
			invalid_items++
			if(messege=='')
			messege="The "+ field_label + " is too short, it should contain as a minimum "+min_length+" characters."	
		}//if 
		
	}//if min

	if(max_length)
	{
		if(field.value.length>max_length){
			invalid_items++
			if(messege=='')
			messege="The "+ field_label + " is too long, it should contain as a max "+max_length+" characters."	
		}//if 
		
	}//if max
	
	if(prevent_special_char)
	{
		 if(field.value.match(/[^a-zA-Z0-9_]/))
		 {
		 	invalid_items++
		 	if(messege=='')
			messege="The "+ field_label + "  should be only alphanumaric."
					 	
		 }//if contain special charters
	}//if It's needed to remove the special characters

	if(document.getElementById(availabilty_field) && document.getElementById(availabilty_field).value==0)
	{
		invalid_items++
		if(messege=='')
		messege="The "+ field_label + " is not available."
	}
	
	
	
	
	if(messege!='')
		if(msg_span!='' && document.getElementById(msg_span))
			document.getElementById(msg_span).innerHTML=messege
		else
			alert(messege)
		

	//------------Ajax that verificate the username
	
			
			
}//username


function validateRePassword(repassword_id,password_id,msg_span)
{
	var messege=''
	var repassword_id=document.getElementById(repassword_id);
	var password_id=document.getElementById(password_id);
	
	if(repassword_id.value!='' &&repassword_id.value!=password_id.value)
	{
		invalid_items++;
		if(messege=='')
		messege="The password and it's retyping are not equal ";
		password_id.value='';
		repassword_id.value='';
	}
	
	
	if(messege!=''){
		
		if(msg_span!='' && document.getElementById(msg_span))
			document.getElementById(msg_span).innerHTML=messege
		else
			alert(messege)
	}//if message is set
	
}



function validateText(field_id,field_label,msg_span,min_length,max_length,prevent_special_char,required)
{
	//This function validate any text fields 
	
	var messege=''
	var field=document.getElementById(field_id);
	if(!field)	return;
	if(required){
		if(field.value.length==0){
			invalid_items++
			if(messege=='')
			messege="Please enter the "+ field_label	
		}//if empty
	}//if required
	if(min_length)
	{
		if(field.value.length<min_length){
			invalid_items++
			if(messege=='')
			messege="The "+ field_label + " is too short, it should contain as a minimum "+min_length+" characters."	
		}//if 
		
	}//if min


	if(max_length)
	{
		if(field.value.length>max_length){
			invalid_items++
			if(messege=='')
			messege="The "+ field_label + " is too long, it should contain as a max "+max_length+" characters."	
		}//if 
		
	}//if max
	
	if(prevent_special_char)
	{
		 if (field.value.match(/[&<>@$]+/) ) 
		 {
		 	invalid_items++
		 	if(messege=='')
			messege="The "+ field_label + " should not contain any special character please remove them."
					 	
		 }//if contain special charters
	}//if It's needed to remove the special characters

	
	if(messege!=''){
		
		if(msg_span!='' && document.getElementById(msg_span))
			document.getElementById(msg_span).innerHTML=messege
		else
			alert(messege)
	}
	
}//function validateText

function validateTelephone(field_id,field_label,msg_span,min_length,max_length,prevent_special_char,required)
{

	var messege=''
	var field=document.getElementById(field_id);
	if(!field)	return;
	
	if(required){
		if(field.value.length==0){
			invalid_items++
			if(messege=='')
			messege="Please enter the "+ field_label	
		}//if empty
	}//if required
	if(min_length)
	{
		if(field.value.length<min_length){
			invalid_items++
			if(messege=='')
			messege="The "+ field_label + " is too short, it should contain as a minimum "+min_length+" numbers."	
		}//if 
		
	}//if min


	if(max_length)
	{
		if(field.value.length>max_length){
			invalid_items++
			if(messege=='')
			messege="The "+ field_label + " is too long, it should contain as a max "+max_length+" numbers."	
		}//if 
		
	}//if max
	
	if(prevent_special_char)
	{
		 if (field.value.match(/[&<>@$]+/) ) 
		 {
		 	invalid_items++
		 	if(messege=='')
			messege="The "+ field_label + " should not contain any special character please remove them."
					 	
		 }//if contain special charters
	}//if It's needed to remove the special characters

	var filter=/^([0-9])+$/
	if (! filter.test(field.value))
	{
		invalid_items++
		if(messege=='')
		messege="The "+ field_label + " should contain numbers only"
		
	}
	if(messege!='')
		if(msg_span!='' && document.getElementById(msg_span))
			document.getElementById(msg_span).innerHTML=messege
		else
			alert(messege)

}

function validateMobile(field_id,field_label,msg_span,min_length,max_length,prevent_special_char,required)
{
	var messege=''
	var field=document.getElementById(field_id);
	if(!field) return;
	if(required){
		if(field.value.length==0){
			invalid_items++
			if(messege=='')
			messege="Please enter the "+ field_label	
		}//if empty
	}//if required
	if(min_length)
	{
		if(field.value.length<min_length){
			invalid_items++
			if(messege=='')
			messege="The "+ field_label + " is too short, it should contain as a minimum "+min_length+" numbers."	
		}//if 
		
	}//if min


	if(max_length)
	{
		if(field.value.length>max_length){
			invalid_items++
			if(messege=='')
			messege="The "+ field_label + " is too long, it should contain as a max "+max_length+" numbers."	
		}//if 
		
	}//if max
	
	if(prevent_special_char)
	{
		 if (field.value.match(/[&<>@$]+/) ) 
		 {
		 	invalid_items++
		 	if(messege=='')
			messege="The "+ field_label + " should not contain any special character please remove them."
					 	
		 }//if contain special charters
	}//if It's needed to remove the special characters

	var filter=/^([0-9])+$/
	if (! filter.test(field.value))
	{
		invalid_items++
		if(messege=='')
		messege="The "+ field_label + " should contain numbers only"
		
	}
	if(messege!='')
		if(msg_span!='' && document.getElementById(msg_span))
			document.getElementById(msg_span).innerHTML=messege
		else
			alert(messege)

}

function validateEmail(field_id,field_label,msg_span,min_length,max_length,prevent_special_char,required)
{
	var messege=''
	var field=document.getElementById(field_id);
	if(!field) return;
	if(required){
		if(field.value.length==0){
			invalid_items++
			if(messege=='')
			messege="Please enter the "+ field_label	
		}//if empty
	}
	if(field.value.length!=0){
		
		if(!field.value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/)) 
		{
			invalid_items++
			if(messege=='')
			messege="The "+ field_label + " is invalid, please enter a valid one."
			
		}
	
		
		
		if(min_length)
		{
			if(field.value.length<min_length){
				invalid_items++
				if(messege=='')
				messege="The "+ field_label + " is too short, it should contain as a minimum "+min_length+"  characters."	
			}//if 
			
		}//if min
	
	
		if(max_length)
		{
			if(field.value.length>max_length){
				invalid_items++
				if(messege=='')
				messege="The "+ field_label + " is too long, it should contain as a max "+max_length+"  characters."	
			}//if 
			
		}//if max
		
		
	
		if(prevent_special_char)
		{
			 if (field.value.match(/[&<>$]+/) ) 
			 {
			 	invalid_items++
			 	if(messege=='')
				messege="The "+ field_label + " should not contain any special character please remove them."
						 	
			 }//if contain special charters
		}//if It's needed to remove the special characters

	}
	
	if(messege!=''){
		if(msg_span!='' && document.getElementById(msg_span))
			document.getElementById(msg_span).innerHTML=messege
		else
			alert(messege)
	}
	
	}//functio

function validateURL(field_id,field_label,msg_span,min_length,max_length,prevent_special_char,required)
{
	//accept http://www.sitename.com
	//and accept www.sitename.com
	var messege=''
	var field=document.getElementById(field_id);
	if(!field) return;
	if(required){
		if(field.value.length==0){
			invalid_items++
			if(messege=='')
			messege="Please enter the "+ field_label	
		}//if empty
	}//if required

	
	var v1 = new RegExp();
		var v2 = new RegExp();
	    v1.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
	    v2.compile("[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
		 if ((!v1.test(field.value)) && (!v2.test(field.value))) {
			invalid_items++
			if(messege=='')
			messege="The "+ field_label + " is invalid, please enter a valid one."
		}
	
	
	if(min_length)
	{
		if(field.value.length<min_length){
			invalid_items++
			if(messege=='')
			messege="The "+ field_label + " is too short, it should contain as a minimum "+min_length+" characters."	
		}//if 
		
	}//if min


	if(max_length)
	{
		if(field.value.length>max_length){
			invalid_items++
			if(messege=='')
			messege="The "+ field_label + " is too long, it should contain as a max "+max_length+" characters."	
		}//if 
		
	}//if max
	
	if(prevent_special_char)
	{
		 if (field.value.match(/[&<>@$]+/) ) 
		 {
		 	invalid_items++
		 	if(messege=='')
			messege="The "+ field_label + " should not contain any special character please remove them."
					 	
		 }//if contain special charters
	}//if It's needed to remove the special characters

	if(messege!='')
		if(msg_span!='' && document.getElementById(msg_span))
			document.getElementById(msg_span).innerHTML=messege
		else
			alert(messege)

}


function get_fck_content(fck_name)
{
	var content=FCKeditorAPI.GetInstance(fck_name).EditorWindow.parent.FCK.GetHTML() ;
	var re= /<\S[^><]*>/g
	content=content.replace(re, "");
	
	//Don't workkkkkkkkkkkkkks ..  See them later
	content=strip_spaces(content);
	content=strip_spaces2(content);
	content=strip_enters(content);
	
	return content;
	
}//get_fck_content

function validateFCKEditor(editor_name,field_label,msg_span,required)
{
		//alert("here");
		var messege=''
		if(required){
				//alert(editor_name);
				
			if(get_fck_content(editor_name).length==0)
			{	invalid_items++
				messege="Please enter the "+ field_label	
			}//
		
		}//if required
		if(messege!='')
		if(msg_span!='' && document.getElementById(msg_span))
			document.getElementById(msg_span).innerHTML=messege
		else
			alert(messege)
	
}// validateFCKEditor



//----------- The same as telephone
//function validateFax()
//{}


function validateRadio(form_id,radio_name,field_label,msg_span)
{
	var messege=''
	var form_id=document.getElementById(form_id);
	if(!form_id) return;
	
	if(! ValidateRadios(form_id , radio_name))
	{
		invalid_items++
		messege="Please select one of the "+ field_label 
	}	
	
	if(messege!='')
	if(msg_span!='' && document.getElementById(msg_span))
		document.getElementById(msg_span).innerHTML=messege
	else
		alert(messege)

}//validateRadio


function ValidateRadios(frm, btnName)
{
	var btn = frm[btnName]
	var valid
	
	for (var x = 0;x < btn.length; x++)
	{
		valid = btn[x].checked
		if (valid) {return true}
	}
	if(!valid)
	{
		return false
	}
}



function validateCheckbox(field_id,field_label,msg_span,min_length,max_length,prevent_special_char,required)
{
	
	
	
}

function validateSelectBox(field_id,field_label,msg_span,required)
{
	var messege=''
	var field=document.getElementById(field_id);
	if(!field)	return;
	if(required){
		if(field.value.length==0){
			invalid_items++
			messege="Please select the "+ field_label	
		}//if empty
	}//if required
	
		
	if(messege!='')
		if(msg_span!='' && document.getElementById(msg_span))
			document.getElementById(msg_span).innerHTML=messege
		else
			alert(messege)
	
}

function validateInt(field_id,field_label,msg_span,min_length,max_length,prevent_special_char,required)
{
	//from http://www.regular-expressions.info/numericranges.html
	//0 or 000..999:  ^[0-9]{1,3}$
	
}

function validateFloat(field_id,field_label,msg_span,min_length,max_length,prevent_special_char,required)
{
	//http://www.regular-expressions.info/floatingpoint.html
	//[-+]?([0-9]*\.[0-9]+|[0-9]+)
	
}

function validateDate(field_id,field_label,msg_span,min_length,max_length,prevent_special_char,required)
{
	
	//format: mm/dd/yyyy:	 	 (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d 
	//format dd-mm-yyyy format:	 (0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d 
	
	
	
}//validateDate

function validateCredit(field_id,field_label,msg_span,min_length,max_length,prevent_special_char,required)
{
	/*
	from http://www.regular-expressions.info/creditcard.html
	Visa: ^4[0-9]{12}(?:[0-9]{3})?$
	MasterCard: ^5[1-5][0-9]{14}$ 
	American Express: ^3[47][0-9]{13}$ 
	Diners Club: ^3(?:0[0-5]|[68][0-9])[0-9]{11}$
	
	
	*/
	
}//validateCredit()



function getExt(filename) {
   var dot_pos = filename.lastIndexOf(".");
   if(dot_pos == -1)
      return "";
   return filename.substr(dot_pos+1).toLowerCase();
}


function validate_multiselect(field_id,field_label,msg_span,required)
{
	var messege="";
    var select_filed=document.getElementById(field_id);
    
    if(required)
    {
		var n = new Array();	
	    var arSelected = new Array();
	    
	    var i=0;        
	    while (select_filed.selectedIndex != -1)
	    {
	      	arSelected.push(select_filed.options[select_filed.selectedIndex].value);
	 		n.push(select_filed.selectedIndex);
	        select_filed.options[select_filed.selectedIndex].selected = false;
	        i++;
	    }
	            
	    
	    if(n.length>0)
	    {
	    	for (var t=0;t < n.length ;t++)
	    	  	 select_filed.options[n[t]].selected =true;
	      }
	           
	     else {
	     	messege="Please Select at least one of the " + field_label;
	
	     	invalid_items++;
	     	if(messege!='')
				if(msg_span!='' && document.getElementById(msg_span))
					document.getElementById(msg_span).innerHTML=messege;
			else
				alert(messege)
	     }
    }//if required..
       	 
}




function end(general_msg_span)
{
	general_msg_span=document.getElementById(general_msg_span);
	
	
	if(invalid_items > 0)
	{	
		if(general_msg_span)
		{
			general_msg_span.className='errorDiv';
			general_msg_span.innerHTML="Please change the following fields";
		}
			
		window.scrollTo(0, 0);
		return false;				
	}//
	else return true;

	
}//function end()


function validateFile(field_id,field_label,msg_span,allowed_extension,required)
{
	var messege=''
	var field=document.getElementById(field_id);
	if (!field) return;
	
	if(required){
		
		if(field.value.length==0){
			
			invalid_items++
			messege="Please enter the "+ field_label	
		}//if empty
	}//if required
	
	if(document.getElementById(field_id).value.length!=0)
	{		
		str=document.getElementById(field_id).value;
		
		allowed_extension_array = allowed_extension.split("-");
		
	//	var OK = new Array ('jpg', 'gif', 'jpeg', 'png', 'bmp', 'tif');
		var ext = getExt(str);
		var fileOK = 0;
	
		for (i = 0; i < allowed_extension_array.length; i++) {
			if (allowed_extension_array[i] == ext) 
	  			fileOK = 1; // one of the file extensions found
			}//for
				
		if (fileOK == 0) 
		{
			invalid_items++;
			messege='Please upload a valid file with one of the following extensions ( ' + allowed_extension.replace(/-/g ," , ") + " )";
		}
		
		
	}
	
	if(messege!=''){
			if(msg_span!='' && document.getElementById(msg_span))
				document.getElementById(msg_span).innerHTML=messege;
			else
				alert(messege);
		}
		
}//function

function ajaxInitiate()
{
	var http = false;

	if(navigator.appName == "Microsoft Internet Explorer")
	{
		http = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		http = new XMLHttpRequest();
	}


	http.abort();

	return http;
}
function checkUsername(username)
{
		http=ajaxInitiate();
		http.open("GET","../ajax/username_validation.php?username="+username, true);


		http.onreadystatechange=function()
		{
			if(http.readyState == 4)
			{
				document.getElementById("availabilty_field").value=http.responseText;
			}
		}

		http.send(null);

}
