// 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="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("onblur", 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);

	
	if(ojb.value == ""){
	errorcode = 1;
	outputerror(ojb, errorcode);
	}
	
	switch(type){
		case "email":
		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);
		}
		break; 
		
		case "internationalphone":
		if(checkInternationalPhone(ojb.value)){
			errorcode = 6;
			outputerror(ojb, errorcode);
		}
		break; 
		
		case "image":
		if(ojb.value == ""){
		errorcode = 7;
		outputerror(ojb, errorcode);
		}
		break; 
	}
	if(errorcode == 0){
		
	 	outputerror(ojb, 0);
		return true;
	}else{
		return false;
	}
	
}

function errorlist(num){
	
	var error = new Array(8);
	
	error[0] = "";
	error[1] = "Please enter a value.";
	error[2] = "Please enter a valid email.";
	error[3] = "Please enter a valid date.";
	error[4] = "Please check box.";
	error[5] = "Please enter number.";
	error[6] = "Please enter correct international telephone number.";
	error[7] = "Please select an image.";
	
	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 = - year -/(?:19|20\d{2})\ -- month --  /(?:0[1-9]|1[0-2])\  ----- day ---  /(?:0[1-9]|[12][0-9]|3[01])/;
		
		
	
        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}).addClass('error');
			errordiv.setHTML(errorlist(errornumber))
			//$('submit').disabled="disabled";
			errordiv.injectBefore(ojbitem);
		}
	}else{
		if(errornumber != 0){
			errordiv.setHTML(errorlist(errornumber))
			//$('submit').disabled="disabled";
		}else{
			errordiv.remove();
			//$('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 dragAlbums(){

			var container = $('allcontent');
			$$('.albumCover').each(function(item){
											
			var temp = new Drag.Move(item,{'container': container});
			
			temp.addEvent('onStart', function() {
					item.setStyle('opacity', .5)
					item.style.zIndex = getHighZIndex()+1
				}, true)
				
			temp.addEvent('onComplete', function() {
				if(!window.ie){
					var LoadFade = new Fx.Style(item, 'opacity' ,{duration:500, wait: false});
					LoadFade.start(.5, 1);
				}else{
					item.setStyle('opacity', 1)
				}
				}, true)
			
});

}
var lastzindex = 0;
function getHighZIndex(ojb)
{		
	var i;
	var iHigh = 0;
	
	if(lastzindex == 0){
	albums = $('content').getElements('div[id=pic]')
	//Element.getElementsByClassName("albumCover")
	//--- cycle through all dialogs on page and determine the highest zIndex ---//
	albums.each(function(item) {
		currZ = $(item).style.zIndex;
		if (currZ > iHigh){
			iHigh = currZ;
			lastzindex = currZ;
		}
		
		
		})
	//alert(eval(iHigh)+1)
	//return iHigh;
	}else{
		iHigh = eval(lastzindex)+1;
		lastzindex = iHigh;
	}
	//$('test').setHTML(eval(iHigh)+1);
	ojb.style.zIndex = eval(iHigh)+1;
	
	
}


function showimagesfade(){
	if($('pic')){
	allpic = $$('#pic');
	
	var myChain = new Chain();
	allpic.each(function(item) { 
		myChain.chain( function(){ 
			fx =  new Fx.Style(item, 'opacity' ,{duration:500, wait: true});
			fx.start(0, 1);
		} ); });
	
	var runChain = function() { 
		myChain.callChain();
		if (myChain.chains.length == 0) { runChain = $clear(timer); } 
	}
	var timer = runChain.periodical(500);
	}
	}		

function fadefix(){
//intro = $('intro');
	if($('intro')){
		if(!window.ie6){
			$('intro').style.backgroundImage="url('images/opacity80.png')";
			$('intro').style.backgroundRepeat="repeat";
		} else {
			$('intro').style.backgroundColor = "#333333";
			$('intro').style.filter = "alpha(opacity=80)";
		}
	}
}

function callTours(data){
	
	if (data){
		toururl = 'inc/inc.hometour.php?s='+ data;
	}else{
		toururl = 'inc/inc.hometour.php';
	}
	
	tourlistojb = $('tourresults').empty().addClass('lbLoading');

		callv = new Ajax(toururl, {
			
			method: 'get',
			update: 'tourresults',
			evalScripts: true,		
			onComplete: function() {
			tourlistojb.removeClass('lbLoading');
			}
			}).request();

}

function callToursAll(data){
	
	if (data){
		toururl = 'inc/inc.tours.php?s='+ data;
	}else{
		toururl = 'inc/inc.tours.php';
	}
	
	tourlistojb = $('tourresults').empty().addClass('lbLoading');

		callv = new Ajax(toururl, {
			
			method: 'get',
			update: 'tourresults',
			evalScripts: true,		
			onComplete: function() {
			tourlistojb.removeClass('lbLoading');
			}
			}).request();

}
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function showHide(layerName,linkObj){
    var obj = document.getElementById(layerName);
    if(obj.style.display=='none'){
        obj.style.display='block'; 
        linkObj.innerHTML = 'Show';
    }else{
        obj.style.display='none';
        linkObj.innerHTML = 'Hide';    
    }

}

window.addEvent('domready', function() {
	$ES('td', '#tabs').setStyle('padding', '0 9px 0 6px');
});
