// JavaScript Document

/// validation functions code starts -------------------------------------------------------------


// need to work
// must be in a form with id=form1
// window.addEvent('domready', addeventallFields); must be included on page
// input should have class="validateimage" second word is the type of validation - to add another style add a " " eg: class="validateimage formBGwhite"
// input need id
// submit button needs to be an image :<img src="../../Scripts/images/saveboxsave.gif" border="0"  onclick="checkallFields()"/>

function navchange(page){
	
	$('menuhome').removeClass('navitem');
	$('menuaboutus').removeClass('navitem');
	$('menuservices').removeClass('navitem');
	$('menupeople').removeClass('navitem');
	$('menuclient').removeClass('navitem');
	$('menucase').removeClass('navitem');
	$('menublogs').removeClass('navitem');
	$('menucontact').removeClass('navitem');
	
	$('menuhome').removeClass('navitem_current');
	$('menuaboutus').removeClass('navitem_current');
	$('menuservices').removeClass('navitem_current');
	$('menupeople').removeClass('navitem_current');
	$('menuclient').removeClass('navitem_current');
	$('menucase').removeClass('navitem_current');
	$('menublogs').removeClass('navitem_current');
	$('menucontact').removeClass('navitem_current');
	
	/* ----------------------------------------------- */
	
	if(page=='home'){
		$('menuhome').addClass('navitem_current');
		document.title = "63G Communications  | home | +44 (0)20 7549 5800 info@63G.com";
		
	}else{
		$('menuhome').addClass('navitem');
	}
	
	if(page=='aboutus'){
		$('menuaboutus').addClass('navitem_current');
		document.title = "63G Communications  | about us | +44 (0)20 7549 5800 info@63G.com";
		
	}else{
		$('menuaboutus').addClass('navitem');
		
	}
	
	
	if(page=='services'){
		$('menuservices').addClass('navitem_current');
		document.title = "63G Communications  | services | +44 (0)20 7549 5800 info@63G.com";
		
	}else{
		$('menuservices').addClass('navitem');
	}
	
	if(page=='people'){
		$('menupeople').addClass('navitem_current');
		document.title = "63G Communications  | people | +44 (0)20 7549 5800 info@63G.com";
		
	}else{
		$('menupeople').addClass('navitem');
	}
	
	if(page=='client'){
		$('menuclient').addClass('navitem_current');
		document.title = "63G Communications  | client | +44 (0)20 7549 5800 info@63G.com";
		
	}else{
		$('menuclient').addClass('navitem');
	}
	
	if(page=='case'){
		$('menucase').addClass('navitem_current');
		document.title = "63G Communications  | case studies | +44 (0)20 7549 5800 info@63G.com";
		
	}else{
		$('menucase').addClass('navitem');
	}
	
	if(page=='blogs'){
		$('menublogs').addClass('navitem_current');
		document.title = "63G Communications  | blogs | +44 (0)20 7549 5800 info@63G.com";
		
	}else{
		$('menublogs').addClass('navitem');
	}
	
	if(page=='contact'){
		$('menucontact').addClass('navitem_current');
		document.title = "63G Communications  | contact | +44 (0)20 7549 5800 info@63G.com";
	}else{
		$('menucontact').addClass('navitem');
	}
	
}

function checkallFields(){
	failed = 0;	
	//fieldarray = $('input.validate');
	//fieldarray = $('form1').getElements('input[class^=validate]');
	fieldarray = $ES('input[class^=validate]')
	
	fieldarray.each(function(el){
							  
		classtype = el.className;
		
		if(classtype.indexOf(" ") != -1){
		stringarray = classtype.split(" ");	
		classtype = stringarray[0];
		}
		classtype = classtype.replace("validate", "");
		
		if(!validateval(el.id ,classtype)){
			failed++;	
		};
						  
	})
	//alert(failed)
	if(failed < 1){
		$('form1').submit();
	}else{
		
		errordiv = document.getElementById('errormessage');
		if(errordiv != null){
			document.getElementById('errormessage').innerHTML="Please fill in missing fields";
		}
		
	}
 
}

function addeventallFields(){
	failed = 0;	
	//fieldarray = $$('.^=validate');
	theform = $('form1');
	//fieldarray = theform.getElements('input[class^=validate]');
	fieldarray = $ES('input[class^=validate]')
	
	
	fieldarray.each(function(el){
		el.addEvent("mouseout", function(event){
			checkfield(el)
		})
	});
	
	fieldarray.each(function(el){
		el.addEvent("keydown", function(event){
			checkfield(el)
		})
	});
	
	fieldarray.each(function(el){
		el.addEvent("change", function(event){
			checkfield(el)
		})
	});
	
 
}

function checkfield(el){
	classtype = el.className;
				
				
				if(classtype.indexOf(" ") != -1){
				stringarray = classtype.split(" ");	
				classtype = stringarray[0];
				}
				
				classtype = classtype.replace("validate", "");
				
				if(!validateval(el.id ,classtype)){
					failed++;	
				};
}
	
	
	
function validateval(ojbid ,type){
	errorcode = 0;
	var ojb = $(ojbid);

	
	switch(type){
		case "name":
		if(ojb.value == ""){
		errorcode = 9;
		outputerror(ojb, errorcode);
		}
		break; 
		
		case "txarea":
		if(ojb.value == ""){
		errorcode = 16;
		outputerror(ojb, errorcode);
		}
		break; 
		
		case "email":
		checkemailaddress(ojb.value)
		if(!IsMatchingEmailAddress(ojb.value)){
			errorcode = 2;
			outputerror(ojb, errorcode);
		}
		break; 
		
		
		case "money":
		
		/* strips any commas*/
		moneyvalue = stripCommas ($(ojbid).value);
		
		if(!isInteger(moneyvalue)){
			errorcode = 13;
			outputerror(ojb, errorcode);
		}
		if(moneyvalue.length < 4){
			errorcode = 13;
			outputerror(ojb, errorcode);
		}
		break; 
		
		case "number":
		if(!isInteger(ojb.value)){
			errorcode = 13;
			outputerror(ojb, errorcode);
		}
		if(ojb.value.length < 4){
			errorcode = 13;
			outputerror(ojb, errorcode);
		}
		break; 
		
		case "date":
		if(!isValidDate(ojb.value)){
			errorcode = 3;
			outputerror(ojb, errorcode);
		}
		break; 
		
		
		case "checkbox":
		if(ojb.checked == ""){
			errorcode = 4;
			outputerror(ojb, errorcode);
		}
		break; 
		
		case "radio":
		if(ojb.value == ""){
			errorcode = 14;
			outputerror(ojb, errorcode);
		}
		break; 
		
		case "phone":
		
		phonevalue = stripPhone ($(ojbid).value);
		
		if(!isInteger(phonevalue)){
			errorcode = 15;
			outputerror(ojb, errorcode);
		}
		if(phonevalue == ""){
		errorcode = 15;
		outputerror(ojb, errorcode);
		}
		break; 
		
		case "internationalphone":
		if(checkInternationalPhone(ojb.value)){
			errorcode = 6;
			outputerror(ojb, errorcode);
		}
		if(ojb.value == ""){
		errorcode = 1;
		outputerror(ojb, errorcode);
		}
		break; 
		
		case "image":
		if(!IsMatchingImage(ojb.value)){
		errorcode = 7;
		outputerror(ojb, errorcode);
		}
		break; 
		
		case "nickname":
		checknickname(ojb.value)
		if(ojb.value == ""){
		errorcode = 10;
		outputerror(ojb, errorcode);
		}
		break; 
		
		case "password1":
		if(ojb.value == ""){
		errorcode = 11;
		outputerror(ojb, errorcode);
		}
		break; 
		
		case "password":
		if(ojb.value != $('pass1').value){
		errorcode = 8;
		outputerror(ojb, errorcode);	
		}
		if(ojb.value == ""){
		errorcode = 12;
		outputerror(ojb, errorcode);
		}
		break; 
		
		default:
		if(ojb.value == ""){
		errorcode = 1;
		outputerror(ojb, errorcode);	
		}
		break; 
	
	}
	if(errorcode == 0){
		
	 	outputerror(ojb, 0);
		return true;
	}else{
		return false;
	}
	
}

function errorlist(num){
	
	error = new Array();
	
	error[0] = "";
	error[1] = "This field must be completed.";
	error[2] = "Please enter a valid email.";
	error[3] = "Please enter a valid date e.g. yyyy-mm-dd.";
	error[4] = "Please check box.";
	error[5] = "Please enter number.";
	error[6] = "Please enter correct international telephone number.";
	error[7] = "Please select an image.";
	error[8] = "Your Passwords do not match.";
	error[9] = "Please enter you name";
	error[10] = "Please enter a nickname";
	error[11] = "Please enter a password";
	error[12] = "Please verify your password";
	error[13] = "Please enter a number to the nearest thousand";
	error[14] = "Please choose payment method";
	error[15] = "Enter valid phone number e.g. 028 1234 1234";
	error[16] = "This field must be completed.";
	

	
	return error[num];
	
}

function IsMatchingImage(str){
    var myImage = /^.*(\.(gif|jpg|bmp|GIF|JPG|BMP))$/;
    return myImage.test(str)
}

function IsMatchingEmailAddress(str){
    var myRegExp = /[a-z0-9-]{1,30}@[a-z0-9-]{1,65}.[a-z]{3}/ ;
    return myRegExp.test(str)
}

function isValidDate(sText) {
		var reDate = /(?:19|20\d{2})\-(?:0[1-9]|1[0-2])\-(?:0[1-9]|[12][0-9]|3[01])/;
        return reDate.test(sText);
}

function outputerror(ojb, errornumber){
	var ojbitem = $(ojb);
	
	errordiv = $('error'+ojb.id)
	//alert(errordiv)
	if(errordiv == null){
		if(errornumber != 0){
			var errordiv = new Element('div', {'id':'error'+ojb.id}).setStyles('color:#CC0000;');
			errordiv.setHTML(errorlist(errornumber))
			//$('submit').disabled="disabled";
			errordiv.injectBefore(ojbitem);
		}
	}else{
		if(errornumber != 0){
			errordiv.setHTML(errorlist(errornumber))
			//$('submit').disabled="disabled";
		}else{
			errordiv.setHTML("")
			//$('submit').disabled="";
		}
	}
}

function stripPhone (numString){
	
	value = numString;
	value = value.replace(/ /g, "");
    return value;
	
}

function stringFilter (input) {
				s = input;
				filteredValues = ", "; // Characters stripped out
				var i;
				var returnString = "";
				for (i = 0; i < s.length; i++) { // Search through string and append to unfiltered values to returnString.
				var c = s.charAt(i);
				if (filteredValues.indexOf(c) == -1) returnString += c;
				}
			return returnString; 
}

function stripCommas(numString) {
	value = numString;
	value = value.replace(/,/g, "");
	value = value.replace(/ /g, "");
    return value;
}

// 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 = 11;

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);
}


/// validation functions code ends -------------------------------------------------------------

function callProduct(id, cat){
	
	if (id){
		Producturl = '../inc/inc.product.php?section='+ cat +'&ID=' + id;
		
	}else{
		Producturl = '../inc/inc.product.php?section='+ cat +'&ID=0';
	}
	
	Productlistojb = $('pic'+id).empty().addClass('lbLoading2');

		callv = new Ajax(Producturl, {
			
			method: 'get',
			update: 'pic'+id,
			evalScripts: true,		
			onComplete: function() {
			Productlistojb.removeClass('lbLoading');
			}
			}).request();
		
}


function checknickname(nickname){
	var myAjax = new Ajax("checknickname.php?NickName="+nickname, {
						  method: 'get',
						  update: 'nicknameerror',
						  evalScripts: true
						  }).request();

}

function checkemailaddress(email){
	var myAjax = new Ajax("checkemail.php?Email="+email, {
						  method: 'get',
						  update: 'erroremail',
						  evalScripts: true
						  }).request();

}



 function showlogin(a){
	 if(a == 1){
		hide = 'none' ;
		show = 'block' ;
		text = "<a href=\"javascript:showlogin(0)\" style=\"color:#0F9DD7;\">Register</a>"
		height = '28px';
		 }else{
		hide = 'block' ;
		show = 'none' ;	 
		text = "Already registered? <a href=\"javascript:showlogin(1)\" style=\"color:#0F9DD7;\">Login</a>"
		height = '105px';
		
	}
	form = document.getElementById("loginform");
	subbut =document.getElementById("sub");
	regbut = document.getElementById("reg");
	butholder = document.getElementById("butholder");
	clicker = document.getElementById("clicker");
	
	clicker.innerHTML = text;
	form.style.display = show;
	subbut.style.display = show;
	butholder.style.marginTop =height;
	regbut.style.display = hide;
}
function redirecttimer(url){
	window.location = url;
}



function resizeDiv() { 
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerHeight ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
				myHeightnum =  (myHeight - 667)
	 		myHeight =  (myHeight - 667) + "px"
  } else if( document.documentElement &&
    ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
				test = document.documentElement.clientHeight;
	 		myHeight =  (myHeight - 667)
				myHeightnum =  (myHeight)
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
				test = document.body.clientHeight;
				
	 		myHeight =  (myHeight - 700)
				myHeightnum =  (myHeight)
  }

		if(myHeightnum > 105){
  document.getElementById("mailer").style.height = myHeight;
		}else{
		document.getElementById("mailer").style.height = '115px';
		}
}



function ielinkfixon(divid){
		container = $(divid).addClass('iefix');
}


function ielinkfixoff(divid){
	container = $(divid).removeClass('iefix');
}
function timerfun(num,divid){
	if(num == 1){
		setTimeout('ielinkfixon("'+divid+'")', 500)
		}else{
			setTimeout('ielinkfixoff("'+divid+'")', 500)
	}
	
	}