
//Function to validate support form
var valid = 1;	
function validateForm(theform)
{
	var valArray = new Array();
	var objArray = new Array("fname","lname","org","desig","email","con","info","ser");
	valid = 1;
	//Data Collection
	//---------------------------------------------------------------------------------------------------------------------------------	
	if(theform.name=="contactForm") {
		 valArray[0] = document.getElementById("firstName").value;
		 valArray[1] = document.getElementById("lastName").value;
		 valArray[2] = document.getElementById("organization").value;
		 valArray[3] = document.getElementById("designation").value;
		 valArray[4] = document.getElementById("emailAddress").value;
		 valArray[5] = document.getElementById("country").value;
		 valArray[6] = document.getElementById("projectInfo").value;
		
		 valArray[7] = document.getElementById("service1");
		 valArray[8] = document.getElementById("service2");
		 valArray[9] = document.getElementById("service3");
		 valArray[10] = document.getElementById("service4");
		 valArray[11] = document.getElementById("service5");
		 valArray[12] = document.getElementById("service6");
		
		
	//---------------------------------------------------------------------------------------------------------------------------------	
	
	//Data Validation
	//---------------------------------------------------------------------------------------------------------------------------------	
		
		for(i=0;i<7;i++){
			isBlank(valArray[i],objArray[i]);
			
		}
		
		
		isSelected(valArray[7],valArray[8],valArray[9],valArray[10],valArray[11],valArray[12]);
		
		if(valid){
			isValidEmail(valArray[4],objArray[4]);
			//To check if user has entered chat Id & not selected instant messanger
			if(valid){
				if(document.getElementById("chat").value!=""){
				
					if(document.getElementById("chatType").value==""){
						valid =0;
						showStatus("Oops! You must select an Instant Messenger.",'statusErr');
						
						document.getElementById("chatlabel").style.backgroundColor="#ffffcc";
						moveUp();
						return false; 
					}else{
						document.getElementById("chatlabel").style.backgroundColor="";
					}
				}
			}
			//-------------------------------------------------------------------------------------------------------------------------
			if(valid){
				return true; 	
			}else{
				showStatus("Oops! You must enter a valid email address.",'statusErr');
				moveUp();
				return false; 	
			}
			
		}else{
			showStatus("Oops! You forgot to enter some vital information.",'statusErr');
			moveUp();
			return false; 	
		}
		
	}
	
	//---------------------------------------------------------------------------------------------------------------------------------	
	
}										

//Function to validate service form
function validateServiceForm(theform)
{
	var valArray = new Array();
	var objArray = new Array("uname","email","com");
	valid = 1;
	//Data Collection
	//---------------------------------------------------------------------------------------------------------------------------------	
	//if(theform.name=="serviceForm") {
		 valArray[0] = document.getElementById("uName").value;
		 valArray[1] = document.getElementById("emailAddress").value;
		 valArray[2] = document.getElementById("comments").value;
		 valArray[3] = document.getElementById("service").value;
			
	//---------------------------------------------------------------------------------------------------------------------------------	
	
	//Data Validation
	//---------------------------------------------------------------------------------------------------------------------------------	
		
		for(i=0;i<3;i++){
			isBlank(valArray[i],objArray[i]);
			
		}
		isSpecial(valArray[0]);
		if(valid){
			isValidEmail(valArray[1],objArray[1]);
			
			
			//-------------------------------------------------------------------------------------------------------------------------
			if(valid){
				submitServiceForm(valArray[0],valArray[1],valArray[2],valArray[3]);
				return false; 	
			}else{
				showStatus("Oops! You must enter a valid email address.",'statusErr');
				return false; 	
			}
			
		}else{
			showStatus("Oops! You forgot to enter some vital information.",'statusErr');
			return false; 	
		}
		
	//}
	
	//---------------------------------------------------------------------------------------------------------------------------------	
	
}										

/*------------------ Submit Service form with Ajax request -------------------------------------------------------------------------- */
function submitServiceForm(name,email,comments,service){
	var dataString = 'uName='+ name + '&uEmail=' + email + '&comments=' + comments + '&service=' + service;  
	
   //alert (dataString);return false;  
    $.ajax({  
    type: "POST",  
    url: "../includes/serviceSubmit.php",  
    data: dataString,  
    
    success: function() {  
       $('#contact_form').fadeOut("slow",function(){
       		$('#message').fadeIn(1500);
       });
    var conversion=document.getElementById("conversion");
    conversion.setAttribute("src", "../includes/conversion.php");
       //alert(conversion.getAttribute("src"));
       //$('#contact_form').html("<div class='thanks'><strong>Thanks for your interest in Paper Plane.</strong> We will get back to you shortly.</div>").hide().fadeIn(1500);  
    } 
     
    });  
    return false;  
}

//---------------------------------------------------------------------------------------------------------------------------------	

/*------------------Function to validate blank value ----------------- */
function isBlank(val,obj)
{
	
	if (val=="" || val==" ")
	{
		valid =0;
		showError(obj);
		
	}else{
		changeProper(obj);
	}
	
	
}//End of isBlank()	


/*------------------Function to validate special characters ----------------- */
function isSpecial(val)
{

var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
//alert(val);
  for (var i = 0; i < val.length; i++) 
  {
  	//alert(iChars.indexOf(val.charAt(i)))
  	if (iChars.indexOf(val.charAt(i)) != -1) 
  	{
  	ShowStatus("Your username has special characters.", 'statusErr');
  	return false;
  	}
  }
}//End of isSpecial()	


/*------------------Function to validate selected value ----------------- */
function isSelected()
{
	
	for(i=0;i<arguments.length;i++){
		if(arguments[i].checked)
			break;
	}
	
	if(i==arguments.length){
		showError("ser");
		valid =0;
	}else{
		changeProper("ser");
	}
	
	
}//End of isSelected()	

/*------------------Function to validate email  ----------------- */
function trim(str)
{ 
	return((""+str).replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') ); 
}

function isValidEmail(value , obj){
		
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if(filter.test(trim(value))){
		changeProper(obj);
	}else{
		valid = 0;
		showError(obj);
	}
	
}//End of isValidEmail()	

/*------------------Function to validate for alphaNumeric value ----------------- */
function isAlphaNumeric(val)
{
	if (val.match(/[a-zA-Z]/)){
		if (val.match(/[0-9]/)){
			return true;
		}
		return false;
	}
	return false;
	
}//End of isAlphaNumeric()	

/*------------------Function to validate length  ----------------- */
function isValidLength(val,minLength,maxLength)
{
	if (val.length > minLength && val.length < maxLength){
			return true;
	}
	return false;
	
}//End of isValidLength()	

/*------------------Function to confirm the password value ----------------- */
function ismatch(newVal,cofirmVal)
{
	if (newVal==cofirmVal)
	{
		return true;
	}
	return false;

}//End of ismatch()	

/*------------------Function to validate for Number  ----------------- */
function isNumber(value){
  var iChars = "0123456789";
  for (var i = 0; i < value.length; i++) {
  	if (iChars.indexOf(value.charAt(i)) == -1) 
  	{
  		return false;
  	}
  }	
  return true;
}//End of isNumber()	


/*------------------Function to hide/show a same div ----------------- */
function toogleElement(EleName,style){
	var objEle = document.getElementById(EleName);
	if(objEle)
		objEle.style.display=style;
}//End of toogleElement()	

/*------------------Function to hide/show a same div ----------------- */
function showNotification(msg){
	showStatus(msg,'statusMsg');
}//End of showNotification()	

function showError(obj){
	
	//showStatus(msg,'statusErr');
	//document.getElementById("firstName").focus();
	changeError(obj);
}//End of ismatch()	

function showStatus(msg,type){
	
	var ele = document.getElementById('msg');
	ele.innerHTML = msg;
	ele.className = type;

	toogleElement('msg','block');
	
	
}//End of showStatus()	

function changeError(id){
	document.getElementById(id).className = "invalid";
	//document.getElementById(id).style.borderStyle="solid";
	//document.getElementById(id).style.borderColor="red";
	
}//End of changeError()	

function changeProper(id){
	document.getElementById(id).className = "";
	//document.getElementById(id).style.borderStyle="";
	//document.getElementById(id).style.borderColor="";
	toogleElement('msg','none');
}//End of changeProper()	


function moveUp(){
   //displayDescription(cType,cValue,nodeNo);
   var h = $("body").height();
   $('html, body').animate({scrollTop:0}, 'slow'); 
}
