// 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 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();
	}
 
}

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 "email":
		checkemailaddress(ojb.value)
		if(!IsMatchingEmailAddress(ojb.value)){
			errorcode = 2;
			outputerror(ojb, errorcode);
		}
		break; 
		
		case "date":
		if(!isValidDate(ojb.value)){
			errorcode = 3;
			outputerror(ojb, errorcode);
		}
		break; 
		
		case "checkbox":
		if(ojb.value == ""){
			errorcode = 4;
			outputerror(ojb, errorcode);
		}
		break; 
		
		case "phone":
		if(!isInteger(ojb.value)){
			errorcode = 5;
			outputerror(ojb, errorcode);
		}
		if(ojb.value == ""){
		errorcode = 1;
		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(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";
	
	return error[num];
	
}

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="";
		}
	}
}

// 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: 'erroremail1',
						  evalScripts: true
						  }).request();

}
//nickname = false;
//function checknickname(nickname){
//	var xmlHttp;
//  // Firefox, Opera 8.0+, Safari
//  try{xmlHttp=new XMLHttpRequest();}
//  catch (e)
//  // Internet Explorer
//    {try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");} 
//    catch (e)
//      {try{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
//      catch (e)
//        {alert("Your browser does not support AJAX!");
//        return false;}}}
//    xmlHttp.open("GET","checknickname.php?NickName="+nickname,true);
//    xmlHttp.send(null);	
//    xmlHttp.onreadystatechange=function()
//      {
//      if(xmlHttp.readyState==4)
//        {
//			if(xmlHttp.responseText == 1){
//			//	alert(false)
//			nickname= false;
//			}else{
//			//	alert(true)
//			nickname= true;	
//			}
//        }
//      }
//
//  }
 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;
	 
}

window.addEvent('domready', function() {
if($('floater')){
				$('floater').setStyle('display','none');
}
var bio = new Fx.Style('floater', 'opacity' ,{duration:500});//animate new page bio
var current = new Fx.Style('maincontent', 'opacity' ,{duration:500});//animate background

$ES('.staffurl').addEvent('click', function(e) { //find all 'class=staffurl' and add an onclick event
	e = new Event(e).stop();

 url = this.href; //pull url from a href (a tag only for SEO)
	if($('floater').getStyle('display') != 'block'){
	
	$('floater').setStyle('display','block');
 
	new Ajax(url, {
		method: 'get',
		update: $('floater'),
		evalScripts: true  //for the scroll bar in the new page loaded
	}).request();
	
	bio.start(0, 1); // animate
	current.start(1, 0); // animate
	}else{
	changebio(url)
	}
		
});


$ES('.popup').addEvent('click', function(e) { //find all 'class=staffurl' and add an onclick event
	e = new Event(e).stop();
	
 url = this.href; //pull url from a href (a tag only for SEO)
 if($('floater').getStyle('display') != 'block'){
	
	$('floater').setStyle('display','block');
	
	new Ajax(url, {
		method: 'get',
		update: $('floater'),
		evalScripts: true  //for the scroll bar in the new page loaded
	}).request();
	
	bio.start(0, 1); // animate
	current.start(1, 0); // animate
	}else{
	changebio(url)
	}
		
});

});


function closebio(){
var bio = new Fx.Style('floater', 'opacity' ,{duration:500});
var current = new Fx.Style('maincontent', 'opacity' ,{duration:500});
	bio.start(1, 0);
	current.start(0, 1);
	$('floater').setStyle('display','none');
}


function changebio(url){
var bio2 = new Fx.Style('floater', 'opacity' ,{duration:500}); //animate with no oncomplete

var bio = new Fx.Style('floater', 'opacity' ,{//animate with oncomplete
duration:500,
	//on complete loading page 
	onComplete: function() {
			
		new Ajax(url, {
		method: 'get',
		update: $('floater'),
		evalScripts: true,
		onComplete: function() {
		//on complete fade in using ainmation with on oncomplete
			bio2.start(0, 1);
		}
	}).request();
	
		}
});
	
	bio.start(1, 0)
	
}

window.addEvent('domready', function() {
if($('floater')){
				$('floater').setStyle('display','none');
}
new MooScroller($E('.product_bagcontent'), $E('.product_scrollKnob'), {
	scrollLinks: {
		forward: $E('.product_scrollForward'),
		back: $E('.product_scrollBack')
	},
	scrollSteps:100
});
});

window.addEvent('domready', function() {
new MooScroller($E('.product_bagcontent2'), $E('.product_scrollKnob2'), {
	scrollLinks: {
		forward: $E('.product_scrollForward2'),
		back: $E('.product_scrollBack2')
	},
	scrollSteps:100
});
});




window.addEvent('domready', function() {
new MooScroller($E('.product_bagcontent3'), $E('.product_scrollKnob3'), {
	scrollLinks: {
		forward: $E('.product_scrollForward3'),
		back: $E('.product_scrollBack3')
	},
	scrollSteps:100
});
});

function loadpage(url) { 
var bio = new Fx.Style('floater', 'opacity' ,{duration:500});
var current = new Fx.Style('maincontent', 'opacity' ,{duration:500});

	if($('floater').getStyle('display') != 'block'){
	
	$('floater').setStyle('display','block');
 
	new Ajax(url, {
		method: 'get',
		update: $('floater'),
		evalScripts: true  //for the scroll bar in the new page loaded
	}).request();
	
	bio.start(0, 1); // animate
	current.start(1, 0); // animate
	}else{
	changebio(url)
	}
		
}