// JavaScript Document

// this is a function for the backend that should change the default photo display in productn manager
function change_display($img){
	document.getElementById('defImg').src = $img;
}

function loading_text(div){
document.getElementById(div).innerHTML = "<div style='width:300px; margin-left:300px; margin-top:20px; text-align:center;'><br/><br/><br/><strong>...Loading...</strong><br/><br/><br/><img src='images/mazloading.gif'/ height='50'></div>";	
}

// validate a form

function validate_form(formName){
	
	if(formName == 'add_to_basket'){ // validate the add to basket form
		var color	  = document.forms[formName].elements['colour_id'].value;
		var quantity  = document.forms[formName].elements['quantity'].value;
		var amount 	  = parseInt(quantity);

			if(color != ''){
				var size  	  = document.forms[formName].elements['product_matrix_id'].value;
			}

			if(color == '' ){
				alert('Please select a colour');
				return false;
			}
			if(size == ''){
				alert('Please select a size');
				return false;
			}
			if(quantity == ''){
				alert('Please tell us how many pairs you want to order');
				return false;
			}
			if(amount < 1){
				alert('Please tell us how many pairs you want to order');
				return false;
			}
			else{
				return true;	
			}
		
	} // end the add to basket validation
	
	
}


	function validate_delivery(){

		if(document.forms['address_form'].elements['delivery_firstname'].value == ''){
			alert('Please enter a first name');
			return false;
		}

		if(document.forms['address_form'].elements['delivery_surname'].value == ''){
			alert('Please enter a surname');
			return false;
		}

		if(document.forms['address_form'].elements['delivery_address1'].value == ''){
			alert('Please enter line one of the address');
			return false;
		}
		if(document.forms['address_form'].elements['delivery_address2'].value == ''){
			alert('Please enter line two of the address');
			return false;
		}
		if(document.forms['address_form'].elements['delivery_town'].value == ''){
			alert('Please enter a town');
			return false;
		}
		if(document.forms['address_form'].elements['delivery_county'].value == ''){
			alert('Please enter a county');
			return false;
		}
		if(document.forms['address_form'].elements['delivery_postcode'].value == ''){
			alert('Please enter a postcode');
			return false;
		}
		if(document.forms['address_form'].elements['delivery_country'].value == ''){
			alert('Please select a country');
			return false;
		}
	
	}


function info_tips(div_holder,content){
	var holder = document.getElementById(div_holder);
	holder.innerHTML = content;
}


function popup(popurl){
var winpops=window.open(popurl,"","width=400,height=250,status,resizable");
}

function popup600(popurl){
var winpops=window.open(popurl,"","width=600,height=600,status,resizable");
}

function popupNotice(popurl){
var winpops=window.open(popurl,"","width=900,height=650,status,resizable,menubar,scrollbars");
}


function popup_big(popurl){
var winpops=window.open(popurl,"","width=600,height=550,status,resizable");
}

function popup_full(popurl){
var winpops=window.open(popurl,"","width=900,height=700,status,scrollbars,resizable");
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Using the popup
function popupDHTML(sent_html,top,middle,bottom){
// This function will apply a transluscent background to your page and apply a white div to appear on top
	
	// First we get the height and width of the visible area
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
    document.getElementById('popup').style.opacity = 0;
    document.getElementById('popup').style.MozOpacity = 0;
    document.getElementById('popup').style.KhtmlOpacity = 0;
    document.getElementById('popup').style.filter = "alpha(opacity=" + 0 + ")";
    document.getElementById('hider').style.opacity = 0;
    document.getElementById('hider').style.MozOpacity = 0;
    document.getElementById('hider').style.KhtmlOpacity = 0;
    document.getElementById('hider').style.filter = "alpha(opacity=" + 0 + ")";

	// Now we have the height and width cross-browser so we can apply them - along with a display attribute
	// to the two elements we are using
	document.getElementById('popup').style.display='block';
	document.getElementById('popup_bottom').style.display='block';
	document.getElementById('hider').style.display='block';
	currentOpac('popup', 100, 500);
	currentOpac('hider', 15, 500);
	document.getElementById('hider').style.height= myHeight+'px';
	document.getElementById('hider').style.width = myWidth+'px';
	
	//document.getElementById('popup_bottom').innerHTML = '<div style="margin-left:5px;"><h2 class="orange_title" style="font-size:13pt; line-height:15pt;">Shipping Costs</h2><br/><br/><br/>Example 1<br/>Example 1<br/>Example 1<br/>Example 1<br/>Example 1<br/>Example 1<br/>Example 1<br/><br/><br/><a class="orange_link" href="#" onClick="javascript:popupDHTMLhide();">Close Window</a></div>';
	document.getElementById('popup_bottom').innerHTML = '<div class="'+top+'"></div><div class="'+middle+'">'+sent_html+'<br/>&nbsp;&nbsp;<a href="#" class="grey_link" onClick="javascript:popupDHTMLhide();">Close Window</a><br/><br/></div><div class="'+bottom+'"></div>';

	
//	document.getElementById('popup_bottom').innerHTML = '';
	//To Debug :¬)
	//window.alert( 'Width = ' + myWidth );
	//window.alert( 'Height = ' + myHeight );
}

function popupDHTMLhide(){
// this function hides the popup
    document.getElementById('popup').style.opacity = 100;
    document.getElementById('popup').style.MozOpacity = 100;
    document.getElementById('popup').style.KhtmlOpacity = 100;
    document.getElementById('popup').style.filter = "alpha(opacity=" + 100 + ")";
	currentOpac('popup', 0, 500);
	currentOpac('popup_bottom', 0, 100);
	currentOpac('hider', 0, 500);
	setTimeout('removeDivs()',1000);
}

function removeDivs(){
	document.getElementById('popup').style.display='none';
	document.getElementById('popup_bottom').style.display='none';
	document.getElementById('hider').style.display='none';
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Opacity transition functions 

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function shiftOpacity(id, millisec) {
    //if an element is invisible, make it visible, else make it ivisible
    if(document.getElementById(id).style.opacity == 0) {
        opacity(id, 0, 100, millisec);
    } else {
        opacity(id, 100, 0, millisec);
    }
} 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function blendimage(divid, imageid, imagefile, millisec) {
    var speed = Math.round(millisec / 100);
    var timer = 0;
    
    //set the current image as background
    document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
    
    //make image transparent
    changeOpac(0, imageid);
    
    //make new image
    document.getElementById(imageid).src = imagefile;

    //fade in image
    for(i = 0; i <= 100; i++) {
        setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
        timer++;
    }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function currentOpac(id, opacEnd, millisec) {
    //standard opacity is 100
    var currentOpac = 100;
    
    //if the element has an opacity set, get it
    if(document.getElementById(id).style.opacity < 100) {
        currentOpac = document.getElementById(id).style.opacity * 100;
    }

    //call for the function that changes the opacity
    opacity(id, currentOpac, opacEnd, millisec)
} 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function repeatFields(){
	if (document.getElementById('repeat_fields').checked == true) {
		document.forms['user_form'].elements['delivery_frontenduser_title'].value 	   = document.forms['user_form'].elements['frontenduser_title'].value;
		document.forms['user_form'].elements['delivery_frontenduser_firstname'].value   = document.forms['user_form'].elements['frontenduser_firstname'].value;
		document.forms['user_form'].elements['delivery_frontenduser_surname'].value 	   = document.forms['user_form'].elements['frontenduser_surname'].value;
		document.forms['user_form'].elements['delivery_frontenduser_address1'].value    = document.forms['user_form'].elements['frontenduser_address1'].value;
		document.forms['user_form'].elements['delivery_frontenduser_address2'].value    = document.forms['user_form'].elements['frontenduser_address2'].value;
		document.forms['user_form'].elements['delivery_frontenduser_address3'].value    = document.forms['user_form'].elements['frontenduser_address3'].value;
		document.forms['user_form'].elements['delivery_frontenduser_town'].value  	   = document.forms['user_form'].elements['frontenduser_town'].value;
		document.forms['user_form'].elements['delivery_frontenduser_postcode'].value    = document.forms['user_form'].elements['frontenduser_postcode'].value;
		document.forms['user_form'].elements['delivery_frontenduser_county'].value 	   = document.forms['user_form'].elements['frontenduser_county'].value;
		document.forms['user_form'].elements['delivery_frontenduser_phone'].value     = document.forms['user_form'].elements['frontenduser_phone'].value;
		document.forms['user_form'].elements['delivery_frontenduser_country'].value     = document.forms['user_form'].elements['frontenduser_country'].value;
	}
	
	if (document.getElementById('repeat_fields').checked == false) {
		document.forms['user_form'].elements['delivery_frontenduser_title'].value 	   = '';
		document.forms['user_form'].elements['delivery_frontenduser_firstname'].value   = '';
		document.forms['user_form'].elements['delivery_frontenduser_surname'].value 	   = '';
		document.forms['user_form'].elements['delivery_frontenduser_address1'].value    = '';
		document.forms['user_form'].elements['delivery_frontenduser_address2'].value    = '';
		document.forms['user_form'].elements['delivery_frontenduser_address3'].value    = '';
		document.forms['user_form'].elements['delivery_frontenduser_town'].value  	   = '';
		document.forms['user_form'].elements['delivery_frontenduser_postcode'].value    = '';
		document.forms['user_form'].elements['delivery_frontenduser_county'].value 	   = '';
		document.forms['user_form'].elements['delivery_frontenduser_phone'].value     = '';
		document.forms['user_form'].elements['delivery_frontenduser_country'].value     = '';
	}
}
