// JavaScript Document
function loadHTML(URL, func){
	//dest = destination;
	if(window.XMLHttpRequest){
		request = new XMLHttpRequest();
		//request.onreadystatechange=processStateChange;
		request.onreadystatechange=eval(func);
		request.open("GET",URL,true);
		request.send(null);
	}else if(window.ActiveXObject){
		request=new ActiveXObject("Microsoft.XMLHTTP");
		if(request){
		//	request.onreadystatechange=processStateChange;
			request.onreadystatechange=eval(func);
			request.open("GET",URL,true);
			request.send(null);
		}
	}
}

//function for City----->
function ddl_city_data(){
	if(request.readyState==4){
		if(request.status==200){
			response = request.responseText;
			if(parseInt(response) == 0){
			  	alert("Unable to query DB ...");
				return false;
			}else{
				if(window.ActiveXObject){
					var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
					xmlDoc.async="false";
					xmlDoc.loadXML(response);
				}else if (document.implementation && document.implementation.createDocument){
					var parser = new DOMParser();
					var xmlDoc = parser.parseFromString(response,"text/xml");
				}
				xmlObj=xmlDoc.documentElement;
				if(xmlObj){
					var total_len = xmlObj.childNodes.length;
					document.getElementById("ddlcity").length = 1;
					document.getElementById("ddlcity").options[0].value = "0";
					document.getElementById("ddlcity").options[0].text = "Select City";
					for(i=1;i-1<total_len;i++){
						var node_len = xmlObj.childNodes[i-1].childNodes.length;
						document.getElementById("ddlcity").length = i + 1;
						document.getElementById("ddlcity").options[i].value = xmlObj.getElementsByTagName('cityid')[i-1].firstChild.nodeValue;
						document.getElementById("ddlcity").options[i].text = xmlObj.getElementsByTagName('city')[i-1].firstChild.nodeValue;
					}
				}else{
					document.getElementById("ddlcity").length = 1;
					document.getElementById("ddlcity").options[0].value = "0";
					document.getElementById("ddlcity").options[0].text = "Select City";
				}
			}
		}else{
			alert("Error - Status : " + request.status);
		}
	}
}
//function for Region
function ddl_region_data(){
	if(request.readyState==4){
		if(request.status==200){
			response = request.responseText;
			if(parseInt(response) == 0){
			  	alert("Unable to query DB ...");
				return false;
			}else{
				if(window.ActiveXObject){
					var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
					xmlDoc.async="false";
					xmlDoc.loadXML(response);
				}else if (document.implementation && document.implementation.createDocument){
					var parser = new DOMParser();
					var xmlDoc = parser.parseFromString(response,"text/xml");
				}
				xmlObj=xmlDoc.documentElement;
				if(xmlObj){
					var total_len = xmlObj.childNodes.length;
					document.getElementById("ddlregion").length = 1;
					document.getElementById("ddlregion").options[0].value = "0";
					document.getElementById("ddlregion").options[0].text = "Select State";
					for(i=1;i-1<total_len;i++){
						var node_len = xmlObj.childNodes[i-1].childNodes.length;
						document.getElementById("ddlregion").length = i + 1;
						document.getElementById("ddlregion").options[i].value = xmlObj.getElementsByTagName('regionid')[i-1].firstChild.nodeValue;
						document.getElementById("ddlregion").options[i].text = xmlObj.getElementsByTagName('region')[i-1].firstChild.nodeValue;
					}
				}else{
					document.getElementById("ddlcountry").length = 1;
					document.getElementById("ddlcountry").options[0].value = "0";
					document.getElementById("ddlcountry").options[0].text = "Select State";
				}
			}
		}else{
			alert("Error - Status : " + request.status);
		}
	}
}
//function for type business 
function business_type_data(){
	if(request.readyState==4){
		if(request.status==200){
			response = request.responseText;
			
			if(parseInt(response) == 0){
			  	alert("Unable to query DB ...");
				return false;
			}else{
				if(window.ActiveXObject){
					var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
					xmlDoc.async="false";
					xmlDoc.loadXML(response);
				}else if (document.implementation && document.implementation.createDocument){
					var parser = new DOMParser();
					var xmlDoc = parser.parseFromString(response,"text/xml");
				}
				xmlObj=xmlDoc.documentElement;
				if(xmlObj){
					var total_len = xmlObj.childNodes.length;
					document.getElementById("contact_type").length = 1;
					document.getElementById("contact_type").options[0].value = "0";
					document.getElementById("contact_type").options[0].text = "Select Your Business Type";
					for(i=1;i-1<total_len;i++){
						var node_len = xmlObj.childNodes[i-1].childNodes.length;
						document.getElementById("contact_type").length = i + 1;
						document.getElementById("contact_type").options[i].value = xmlObj.getElementsByTagName('business_type')[i-1].firstChild.nodeValue;
						document.getElementById("contact_type").options[i].text = xmlObj.getElementsByTagName('business_type')[i-1].firstChild.nodeValue;
					}
				}else{
					document.getElementById("contact_type").length = 1;
					document.getElementById("contact_type").options[0].value = "0";
					document.getElementById("contact_type").options[0].text = "Select Your Business Type";
				}
			}
		}else{
			alert("Error - Status : " + request.status);
		}
	}
}

//function for Calender
function event_data(){
	if(request.readyState==4){
		//contentDiv = document.getElementById(dest);
		if(request.status==200){
			response = request.responseText;
			if(parseInt(response) == 0){
			  	alert("Unable to query DB ...");
				return false;
			}else{
				document.getElementById("dvEvent").innerHTML = response;
			}
		}else{
			//contentDiv.innerHTML = "Error - Status : " + request.status;
			alert("Error - Status : " + request.status);
		}
	}
}

function load_ddl_country(){
	loadHTML("wrapper.php?func=return_country_data&param=" + document.getElementById("ddlcountry").options[document.getElementById("ddlcountry").selectedIndex].value, "ddl_country_data");
}

function load_ddl_region(){
	loadHTML("wrapper.php?func=return_region_data&param=" + document.getElementById("ddlcountry").options[document.getElementById("ddlcountry").selectedIndex].value, "ddl_region_data");
}

function load_ddl_city(){
	loadHTML("wrapper.php?func=return_city_data&param=" + document.getElementById("ddlregion").options[document.getElementById("ddlregion").selectedIndex].value, "ddl_city_data");
}

function load_business_type(){
	loadHTML("wrapper.php?func=return_business_type_data&param=" + document.getElementById("ddlcity").options[document.getElementById("ddlcity").selectedIndex].value, "business_type_data");
}

function validate()
{
	//school name 
	if (document.addschool.schoolname.value == "")
	{
	  alert("Please type School Name")
	  document.addschool.schoolname.focus()
	  return false
	}
	//Sub group
	if (document.addschool.sub_grp.selectedIndex == 0)
	{
	  alert("Please type Sub Group")
	  document.addschool.sub_grp.focus()
	  return false
	}
	//School Overview
	if (document.addschool.schooloverview.value == "")
	{
	  alert("Please type School Overview")
	  document.addschool.schooloverview.focus()
	  return false
	}
	//School courses
	if (document.addschool.schooloverview.value == "")
	{
	  alert("Please type School Overview")
	  document.addschool.schooloverview.focus()
	  return false
	}

	//Street
	if (document.addschool.street.value == "")
	{
	  alert("Please type Street")
	  document.addschool.street.focus()
	  return false
	}
	//Country
	if (document.addschool.ddlcountry.selectedIndex == 0)
	{
	  alert("Please type Country Name")
	  document.addschool.ddlcountry.focus()
	  return false
	}
	//state
	if (document.addschool.ddlregion.selectedIndex == 0)
	{
	  alert("Please Select State")
	  document.addschool.ddlregion.focus()
	  return false
	}
	//pincode
	if (document.addschool.pincode.value == "")
	{
	  alert("Please type Pincode")
	  document.addschool.pincode.focus()
	  return false
	}
	//email
	var email=document.addschool.email
	if ((email.value==null)||(email.value=="")){
		alert("Please Enter your Email ID")
		email.focus()
		return false
	}
	if (echeck(email.value)==false){
		email.value=""
		email.focus()
		return false
	}
	//check phone Number
	var Phone=document.addschool.phone_w
	if ((Phone.value==null)||(Phone.value=="")){
		alert("Please Enter your Phone Number")
		Phone.focus()
		return false
	}
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please Enter a Valid Phone Number")
		Phone.value=""
		Phone.focus()
		return false
	}
	if (document.addschool.cimage.value == "")
	{
	  alert("Please Enter Image Value ")
	  document.addschool.cimage.focus()
	  return false
	}
	
}

function businessvalidate()
{
	if (document.business.business_name.value == "")
	{
	  alert("Please type Business Name")
	  document.business.business_name.focus()
	  return false
	}
	if (document.business.business_desc.value == "")
	{
	  alert("Please type Business Description")
	  document.business.business_desc.focus()
	  return false
	}

	if (document.business.contact_type.selectedIndex == 0)
	{
	  alert("Please type Business Type")
	  document.business.contact_type.focus()
	  return false
	}
	if (document.business.contact_name.value == "")
	{
	  alert("Please type Contact Name")
	  document.business.contact_name.focus()
	  return false
	}
	var email=document.business.email
	if ((email.value==null)||(email.value=="")){
		alert("Please Enter your Email ID")
		email.focus()
		return false
	}
	if (echeck(email.value)==false){
		email.value=""
		email.focus()
		return false
	}
	//Country
	if (document.business.ddlcountry.selectedIndex == 0)
	{
	  alert("Please Select Country Name")
	  document.business.ddlcountry.focus()
	  return false
	}
	if (document.business.ddlregion.selectedIndex == 0)
	{
	  alert("Please Select State Name")
	  document.business.ddlregion.focus()
	  return false
	}
	if (document.business.ddlcity.selectedIndex == 0)
	{
	  alert("Please Select City Name")
	  document.business.ddlcity.focus()
	  return false
	}
	if (document.business.cimage.value == "")
	{
	  alert("Please Enter Image Value ")
	  document.business.cimage.focus()
	  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){
		   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					
	}

var digits = "0123456789";
var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 10;
function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}
	//check City if other than call function
function checkcity()
{
	if (document.business.ddlcity.selectedIndex == 0)
	{
		document.business.ddlcity2.disabled = false;
		document.business.ddlcity2.style.visibility = "visible"
		document.business.ddlcity2.focus()
	}
	else
	{
		document.business.ddlcity2.disabled = true;
		document.business.ddlcity2.style.visibility = "hidden"
	}
}
