// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "-";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldphoneChars = phoneNumberDelimiters + "";
// Minimum no of digits in an international phone no.
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 checkDashes(string)
{
	var dashes = 0
	var dash = "-"
	for (i=0;i < string.length ; i++)
	{
		var singleChar = string.charAt(i);
		if (singleChar == dash)
		{
			var dashes = dashes + 1
		}
	}
	if (dashes == 2)
		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)
{
	var s=stripCharsInBag(strphone,validWorldphoneChars)
	var dashes = checkDashes(strphone)
	return (isInteger(s) && s.length >= minDigitsInIphoneNumber && dashes == true);
	
}

function CheckPhoneNumber(TheNumber) {
var valid = 1
var GoodChars = "0123456789- "
var i = 0
if (TheNumber=="") {
// Return false if number is empty
valid = 0
	}
for (i =0; i <= TheNumber.length -1; i++) {
if (GoodChars.indexOf(TheNumber.charAt(i)) == -1) {
// Note: Remove the comments from the following line to see this
// for loop in action.
//alert(TheNumber.charAt(i) + " is no good.")
valid = 0
} // End if statement
	} // End for loop
return valid
}


// AJAX START

var h_max_requests = 30;
var h_request = new Array(h_max_requests);
var request_count=0;

// Function for generic http calls
function httpCall(url, targetFunction, asynch) {
	//alert('test');
        try {
                // Get next handle.. Loops because all handles may be busy.
                current_handle = next_handle();
                if (window.XMLHttpRequest) {
                        h_request[current_handle] = new XMLHttpRequest();
                        if(targetFunction != null) {
                                eval('f = function() { try {' + targetFunction + '(' + current_handle + ');} catch(e) {} }');
                                h_request[current_handle].onreadystatechange = f;
                        }
                        h_request[current_handle].open("GET", url, asynch);
                        h_request[current_handle].send(null);
				} else if (window.ActiveXObject) {
                        h_request[current_handle] = new ActiveXObject("Microsoft.XMLHTTP");
                        if(targetFunction != null) {
                                eval('f = function() { ' + targetFunction + '(' + current_handle + '); }');
                                h_request[current_handle].onreadystatechange = f;
                        }
                        h_request[current_handle].open("GET", url, asynch);
                        h_request[current_handle].send();
                }
        } catch(e) {
                alert("There was an error perfoming an http call. ");
        }
}

// frees a specific handle
function free_handle(h_handle) {
        h_request[h_handle] = null;
        //alert('Http requests: '+(--request_count)+' out of '+h_max_requests);
}

// get next available handle
function next_handle() {
        var x;
        for(x =0;x<h_max_requests;x++)
                if(h_request[x] == null) {
                        //alert('Http requests: '+(++request_count)+' out of '+h_max_requests);
                        return x;
                }
        return null;
}

function receiveData(h_handle) {
   if (h_request[h_handle].readyState==4) {
     if (h_request[h_handle].status==200) {
     document.getElementById('citys').innerHTML = h_request[h_handle].responseText;
       free_handle(h_handle);
     }
   }
 }
 function indexCitys(h_handle) {
   if (h_request[h_handle].readyState==4) {
     if (h_request[h_handle].status==200) {
     document.getElementById('indexCitys').innerHTML = h_request[h_handle].responseText;
       free_handle(h_handle);
     }
   }
 }
 function serviceCitys(h_handle) {
   if (h_request[h_handle].readyState==4) {
     if (h_request[h_handle].status==200) {
     document.getElementById('serviceCitys').innerHTML = h_request[h_handle].responseText;
       free_handle(h_handle);
     }
   }
 }

 

function check_email(thisemail)
{


	if (thisemail.match(/\b[A-Z0-9._%-]+@[A-Z0-9-]+\.[A-Z]{2,4}\b/i))
		return true;
	else
		return false;
}

function verify_listing_add()
{
	var address = document.getElementById('address').value;
	var phone = document.getElementById('phone').value;
	var fax = document.getElementById('fax').value;
	var email = document.getElementById('address').value;
	
	
	// Build Error Message
	var msg = "The following problems occured \n";
	var good=true;
	
	if (email == "")
	{
	 	msg = msg + "Bad Email \n";
		good=false;
	}
	if (address=="")
	{
		msg = msg + "Bad Address \n";
		good=false;
	}
	
	var loadMaterials = allSelect(1,'selected',',');
	
	if (loadMaterials == false)
	{
		msg = msg + "No Materials Selected \n"
		good=false;
	}
	else
	{
		document.getElementById('materials').value = loadMaterials;
	}
		
	if (good==true)
	{
		document.listing_add.submit();
	}
	else
	{
		alert(msg);
	}
	
}
function deletepic(pgid,picid,description)
{
	if (confirm('*PICTURE WILL BE DELETED \n Do you want to delete pic '+picid+'? \n Description: '+description))
	{
	    window.location = './index.php?m=projects&pjid='+pgid+'&func=deletepic&picid='+picid;
	}
}

function helptopic(id)
{
	alert('test');
}
function loadcurhelp(id)
{
	window.location='./help.php?hid='+id;
}
function loadprojectpictures()
{
	myRef = window.open('./_popups/projectpictures.php','mywin','left=20,top=20,width=405,height=400,toolbar=1,resizable=0');
}
function profilemenu(num)
{
	document.getElementById('yourwebsite').tabber.tabShow(num);
}
function gotoprofile(page)
{
	window.location = 'index.php?m=changeprofile&show='+page;
}
function disablebutton(field,button)
{
	
	var obj1 = document.getElementById(field).value;
	if (obj1 == "")
	{
		document.getElementById(button).disabled = true;
	}
	else
	{
		document.getElementById(button).disabled = false;
	}
}

function validateemailcompany(){
	var name = document.contact.name.value;
	var email = document.contact.email.value;
	var subject = document.contact.subject.value;
	var message = document.contact.message.value;
	at=document.contact.email.value.indexOf("@")
	period=document.contact.email.value.indexOf(".")
	var log = "The follow fields need fixing \n";
	var fail = false;
	if (name=="")
	{
		log += "Name \n";
		fail = true;
	}
	if (subject=="")
	{
		log += "Subject \n";
		fail = true;
	}
	if (email=="")
	{
		log += "Email \n";
		fail = true;
	}
	else if(check_email(document.contact.email.value) == false)
	{
		log += "Invalid Email \n";
		fail = true;
	}
	if (message=="")
	{
		log += "Message \n";
		fail = true;
	}
	
	if (fail == true)
	{
		alert(log);
		return(false);
	}
	else
	{
		alert('Your message was sent!');
		document.contact.submit();
	}
}

function cpopup(page,width,height)
{
	myRef = window.open('./?mode=cpopup&page='+page,'mywin','left=20,top=20,width='+width+',height='+height+',toolbar=1,resizable=1');
}

function cpmode(page,width,height)
{
	myRef = window.open('./?mode='+page,'mywin','left=20,top=20,width='+width+',height='+height+',toolbar=1,resizable=1');
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}


// Save Project
function delfile(id)
{
	
	if (confirm('Are you sure?'))
	{
		var dbDelete = new Ajax.Request('ajax/deleteprojectpicture.php?file='+id,'')
		document.getElementById(id).style.display = 'none';
	}
	
}


function promoEditor(){
	var newcode = prompt("What do I want my promocode to be?", "");	
}