// JavaScript Document

// Apply the Highslide settings
hs.graphicsDir = 'js/highslide/graphics/';
hs.outlineType = 'rounded-white';
hs.wrapperClassName = 'draggable-header no-footer';
hs.allowSizeReduction = false;
// always use this with flash, else the movie will be stopped on close:
hs.preserveContent = false;
hs.showCredits = false;
/**/





var active = 'A';
var gallery = Array();
var gallery_position = 0;

function changeDest(active,inactive){
	document.getElementById(active).className = "selected";
	document.getElementById(inactive).className = "unselected";
	document.getElementById("dest_order").value = active;
}

function changeActive(the_active){
	document.getElementById(active).className='';
	document.getElementById(the_active).className='active_letter';
	active = the_active;
}

function switchSource(source,the_id){
	document.getElementById(the_id).src = source;
	//alert(id+" <- ID ------- Source -> "+source);
}

function gallerySwitch(id,counter){
	//first swap the image that is selected with the image that is active
	/*switchSource('includes/php/image.php?size=50&square=true&url=../../'+gallery[gallery_position],id); */
	//make the selected image active
	switchSource('includes/php/image.php?size=300&url=../../'+gallery[counter],'profile_pic');
	//swap the two images position in array
	/*var dummy = gallery[counter];
	gallery[counter] = gallery[gallery_position];
	gallery[gallery_position] = dummy;*/
	// set the gallery position to counter
	gallery_position = counter;
	
}

function galleryNext(){
	if(gallery_position < gallery.length-1){
		gallery_position++;
	}
	else{
		gallery_position = 0;		
	}
	switchSource('includes/php/image.php?size=300&url=../../'+gallery[gallery_position],'profile_pic');	
}

function loadGallery(src){
	//alert(src);
	gallery[gallery.length] = src;
}

function toggle(element,targets){

	if(element.checked){
		for(var i = 0; i < targets.length; i++){
			targets[i].style.display="block";
		}
	}
	else{
		for(var i = 0; i < targets.length; i++){
			targets[i].style.display="none";
		}
	}
}





function get_extension(text){
	var l = text.length;
	var i = text.lastIndexOf(".");
	
	if(l !=0 && i != -1){
		return text.substr(i+1,l);
	}
	else{
		return false;
	}
}

function accepted_extensions(type,ext){
	var extensions = new Array();
	extensions["video"] = Array("mpg","mpeg","mp4","mpga");
	extensions["image"] = Array("jpg","jpeg","png","gif");
	
	for(var i in extensions[type]){
		alert(i);
		if(i == ext){
			return true
		}
	}
	return false;
}

function validate_uploads(id){
	alert("IT WAS CALLED");
	var text = document.getElementById(id);
	var type = document.getElementById("mediatype");
	if(accepted_extensions(type.value,get_extension(text.value))){
		alert("IT SHOULD RETURN TRUE");
		return true;
	}
	else{
		alert("Please Enter A Correct File Format! ."+get_extension(text.value)+" is an invalid extension");
		return false;
	}
}


function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }




 function hidediv(thediv) {
		if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(thediv).style.display= 'none'
		}
		else {
			if (document.layers) { // Netscape 4
			document.thediv.display= 'none'
			}
			else { // IE 4
			document.all.thediv.style.display= 'none'
			}
		}
	}
	function showdiv(thediv) {
		if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(thediv).style.display = 'block';
		}
		else {
			if (document.layers) { // Netscape 4
			document.thediv.display = 'block';
			}
			else { // IE 4
			document.all.thediv.style.display = 'block';
			}
		}
	}
	
	function hidevisible(thediv) {
		if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(thediv).style.visibility= 'hidden'
		}
		else {
			if (document.layers) { // Netscape 4
			document.thediv.visibility= 'hidden'
			}
			else { // IE 4
			document.all.thediv.style.visibility= 'hidden'
			}
		}
	}
	function showvisible(thediv) {
		if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(thediv).style.visibility = 'visible';
		}
		else {
			if (document.layers) { // Netscape 4
			document.thediv.visibility = 'visible';
			}
			else { // IE 4
			document.all.thediv.style.visibility = 'visible';
			}
		}
	}
	
	
function in_array (needle, haystack, argStrict) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true
    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
 
    var key = '', strict = !!argStrict;
 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
 
    return false;
}

function enableField(id)
{
	id.disabled=false;
}

function setfocus(id) {
   document.getElementById(id).focus()
}


function disableField(id){
	id.disabled=true;
}


function regionInsert(region){
	showdiv('radio_buttons'); 
	document.getElementById('trip_radio').checked = true;  
	showdiv('search_message'); 
	document.getElementById('search_message').value = region;
}


function searchRadioClick(){
	showdiv('search_message'); 
	setfocus('search_message'); 
	if(document.getElementById('search_message').value.length==0){
		document.getElementById('search_message').value='Enter Your Search Here';
	}
	selectAll('search_message');
}

function MM_validateForm() { //v4.0
	if (document.getElementById){
		var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
		for (i=0; i<(args.length-2); i+=3) { 
		  test=args[i+2]; 
		  val=document.getElementById(args[i]);
		  if (val) { 
		  		
				nm=val.name; 
				if ((val=val.value)!="") {
					if (test.indexOf('isEmail')!=-1) { 
						p=val.indexOf('@');
						if (p<1 || p==(val.length-1)){
							errors+='- '+nm+' must contain an e-mail address.\n';
						}		
					} else if (test!='R') { 
						num = parseFloat(val);
						if (isNaN(val)) {
						  errors+='- '+nm+' must contain a number.\n';
						}
						if (test.indexOf('inRange') != -1) { 
							p=test.indexOf(':');
							min=test.substring(8,p); max=test.substring(p+1);
							if (num<min || max<num){
								errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
							}
						} 
					} 
				} else if (test.charAt(0) == 'R'){
					errors += '- '+nm+' is required.\n'; 
				}
		   }
		} 
		if (errors){
			alert('The following error(s) occurred:\n'+errors);
		}
		document.MM_returnValue = (errors == '');
	} 
}


function check_sec(field1,field2,enable){
	x = document.getElementById(field1);
	y = document.getElementById(field2);
	z = document.getElementById(enable);
	//alert(x.value+" = "+y.value);
	if(x.value == y.value){
		enableField(z);
	}
	else{
		disableField(z);
	}
}


function stripAlphaChars(pstrSource) 
{ 
var m_strOut = new String(pstrSource.value);
    m_strOut = m_strOut.replace(/[^0-9]/g, ''); 

    pstrSource.value = m_strOut; 
}

function stripAlpha(pstrSource) 
{ 
var m_strOut = new String(pstrSource.value);
    m_strOut = m_strOut.replace(/[^0-9]/g, ''); 

    return m_strOut; 
}


function addCommas(amount)
{
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	//alert(a);
	var d = a[1];
	//alert(d);
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}
// end of function CommaFormatted()

function formatCurrency(amount)
{
	var i = parseFloat(amount);
	alert(i);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	alert(i);
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	alert(s);
	return s;
}
// end of function CurrencyFormatted()



function formatDollars(field){
	var amount = field.value;
	var a = amount.split('.',2)
	a = a.join('');
	//alert(a.length);
	var cents = a.slice(a.length-1); 
	var dollars = a.slice(0,a.length-1);
	//alert(dollars+"."+cents);
	
	var offset = dollars.length % 3;
	var output = dollars.slice(0,offset);
	
	for(i=offset; i<dollars.length; i+=3){
		output += ","+dollars.slice(offset,offset+3);
	}
	
	field.value = output+"."+cents;
}


function mask(str,textbox,loc,delim){
var locs = loc.split(',');

for (var i = 0; i <= locs.length; i++){
	for (var k = 0; k <= str.length; k++){
	 if (k == locs[i]){
	  if (str.substring(k, k+1) != delim){
	    str = str.substring(0,k) + delim + str.substring(k,str.length)
	  }
	 }
	}
 }
textbox.value = str
}




function capWords(field) {
var tmpStr, tmpChar, preString, postString, strlen;
tmpStr = field.value.toLowerCase();
stringLen = tmpStr.length;
if (stringLen > 0)
{
  for (i = 0; i < stringLen; i++)
  {
    if (i == 0)
	{
      tmpChar = tmpStr.substring(0,1).toUpperCase();
      postString = tmpStr.substring(1,stringLen);
      tmpStr = tmpChar + postString;
    }
    else
	{
      tmpChar = tmpStr.substring(i,i+1);
      if (tmpChar == " " && i < (stringLen-1))
	  {
      tmpChar = tmpStr.substring(i+1,i+2).toUpperCase();
      preString = tmpStr.substring(0,i+1);
      postString = tmpStr.substring(i+2,stringLen);
      tmpStr = preString + tmpChar + postString;
      }
    }
  }
}
field.value = tmpStr;
}







var n;
var p;
var p1







function ValidatePhone(){
	p=p1.value
	if(p.length==3){
		pp=p;
		d4=p.indexOf('(')
		d5=p.indexOf(')')
		if(d4==-1){
			pp="("+pp;
		}
		if(d5==-1){
			pp=pp+")";
		}
		//pp="("+pp+")";
		p1.value="";
		p1.value=pp;
	}
	if(p.length>3){
		d1=p.indexOf('(')
		d2=p.indexOf(')')
		if (d2==-1){
			l30=p.length;
			p30=p.substring(0,3);
			//alert(p30);
			p30=p30+")"
			p31=p.substring(3,l30);
			pp=p30+p31;
			//alert(p31);
			p1.value="";
			p1.value=pp;
		}
	}
	if(p.length>5){
		p11=p.substring(d1+1,d2);
		if(p11.length>3){
			p12=p11;
			l12=p12.length;
			l15=p.length
			p13=p11.substring(0,3);
			p14=p11.substring(3,l12);
			p15=p.substring(d2+1,l15);
			p1.value="";
			pp="("+p13+")"+p14+p15;
			p1.value=pp;
		}
		l16=p.length;
		p16=p.substring(d2+1,l16);
		l17=p16.length;
		if(l17>3&&p16.indexOf('-')==-1){
			p17=p.substring(d2,d2+4);
			p18=p.substring(d2+4,l16);
			p19=p.substring(0,d2);
			pp=p19+p17+"-"+p18;
			p1.value="";
			p1.value=pp;
		}
	}
}


function getIt(m){
	n=m.name;
	//p1=document.forms[0].elements[n]
	p1=m
	ValidatePhone()
}


function testphone(obj1){
	p=obj1.value
	//alert(p)
	p=p.replace("(","")
	p=p.replace(")","")
	p=p.replace("-","")
	p=p.replace("-","")
	//alert(isNaN(p))
	if (isNaN(p)==true){
		alert("Check phone");
		return false;
	}
}


function phoneFilter(field,e) {
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if(code == '8' || code == '9' || code == '17' || code == '46' || code == '37' || code == '38' || code == '39' || code == '40'){
		return true;
	}
	if((code >= 96 && code <= 105) || (code >= 48 && code <= 57)){
		return true;
	}
	else{
		return false;
	}
	/*var character = String.fromCharCode(code);
	var c = parseInt(character);
	//alert('Character was ' + character);
	switch(c){
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
			//alert('is a number');
			//getIt(field);
			return true;
			break;
		default:
			return false;
			break;
	}
	*/
}  


function formatPhone(source){
	text = stripAlpha(source);
	//text += c;
	//alert('');
	if(text.length == 0){
		source.value ='';
	}
	else{
		if(text.length > 0 && text.length <= 3){
			output = "("+text;
			//alert(text+" CASE 1");
		}
		else if(text.length > 3 && text.length <= 6){
			output = "("+text.substring(0,3)+")"+text.substring(3);
		}
		else if(text.length > 6 && text.length <10){
			var part1 = text.substring(0,3);
			var part2 = text.substring(3,6);
			var part3 = text.substring(6);
			output = "("+part1+")"+part2+"-"+part3;
			//alert(text+" CASE 3");
		}
		else if(text.length >= 10){
			output = '(' + text.substring(0,3) + ')' + text.substring(3,6) + '-' + text.substring(6,10);
		}
		
		var lastparen = output.indexOf(')');
		if( lastparen > 0){
			output = output.substring(0,lastparen+1)+" "+output.substring(lastparen+1);
		}
		
		source.value = output;
	}
}


function formatPhone2(F){
	var PH = F.value;
	PH = PH.replace("(","");
	PH = PH.replace(")","");
	PH = PH.replace("-","");
	PH = PH.replace(" ","");
	
	if(PH.length < 3)
	{
		F.value = "(" + PH;
	}
	if(PH.length > 3 && PH.length <= 6)
	{
		F.value = "(" + PH.substring(0,3) + ") " + PH.substring(3,6);
	}
	if(PH.length > 6)
	{
		F.value = "(" + PH.substring(0,3) + ") " + PH.substring(3,6) + "-" + PH.substring(6,10);
	}
}


function checkFieldLength(name){
	//alert(name);
	var elems = document.getElementsByName(name);
	for(i = 0; i < elems.length; i++){
		//alert(elems[i].value.length);
		if(elems[i].value.length > 0){
			return true;
		}
	}
	return false;
}


function selectAll(id)
{
	document.getElementById(id).select();
}

function closeAllDivs(){
	var divs = Array("walu_div","kobi_div","kylie_div","piper_div");
	for(i = 0; i< divs.length; i++){
		hidediv(divs[i]);
	}
}

