// JavaScript Document
window.addEvent('domready', function(){

	var site = Cookie.read('site');

if (Browser.Engine.trident4){
		DD_belatedPNG.fix('.main, .vsp-nav-home-img, .vsp-nav-down-img, .vsp-nav-doc-img, .vsp-nav-spec-img, .vsp-nav-reg-img, .vsp-nav-contact-img, .vsp-footer-bot');}
		
	/*if the right option is available, show the line that explains how to use it*/
	
	if($('loginleftbox')){$$('.bothlog').setStyle('display','block');}
	
 	/* If bprp contact form, lauch the formcheck */
	if($('registerForm')){new FormCheck('registerForm');}
	
	vspInitTabNav();	
	
	/* Scripts dedicated to the contact page - VIRS*/
		if($('applicshow')){
			$('applicshow').addEvent('mouseover', function(){
							show('applicmod');
							})
			$('applicshow').addEvent('mouseout', function(){
							show('applicmod');})
			$('countrieshow').addEvent('mouseover', function(){
						show('countries');})
			$('countrieshow').addEvent('mouseout', function(){
						show('countries');})
			$('telshow').addEvent('mouseover', function(){
						show('countries');})
			$('telshow').addEvent('mouseout', function(){
						show('countries');})
			$('telshow2').addEvent('mouseover', function(){
						show('tel');})
			$('telshow2').addEvent('mouseout', function(){
						show('tel');})			
		};
		
	
		if($('ssdFilter_SearchKeyWord')){
			$('ssdFilter_SearchKeyWord').value=searchttext;
			$('ssdFilter_SearchKeyWord').addEvent('focus', function(){$('ssdFilter_SearchKeyWord').value="";}); 
			/* click event necessary for ie*/
			$('ssdFilter_SearchKeyWord').addEvent('click', function(){$('ssdFilter_SearchKeyWord').value="";}); 
         //$('ssdFilter_SearchKeyWord').addEvent('blur', function(){$('ssdFilter_SearchKeyWord').value=;});
		}
	//	if($('fbl_txt')){
		//	$('fbl_txt').innerHTML=feedbacktext;
		//	$('fbl_txt').addEvent('keydown', function(){limitText(this.form.fbl_txt,this.form.countdown,500);}); 
		//	$('fbl_txt').addEvent('keyup', function(){limitText(this.form.fbl_txt,this.form.countdown,500);}); 
			
			/* click event necessary for ie instead of focus 07/10/09 SF:remove text
			$('fbl_txt').addEvent('click', function(){
				if($('fbl_txt').value == feedbacktext){$('fbl_txt').value="";}
													}); */
	//	}

		if($('help_disp')){
			
			
			$('help_disp').addEvent('mouseover', function(){
						var theIMG = new Element ( 'img' , { 'src' : '/images/computing/vaio/display.jpg'});
						theIMG.injectInside($('image_help'));});
			
			$('help_orig').addEvent('mouseover', function(){
						var theIMG = new Element ( 'img' , { 'src' : '/images/computing/vaio/modelcode.jpg'});
						theIMG.injectInside($('image_help'));});
			
			$('help_sys').addEvent('mouseover', function(){
						var theIMG = new Element ( 'img' , { 'src' : '/images/computing/vaio/VCC.jpg'});
						theIMG.injectInside($('image_help'));});			
			
			$('help_disp').addEvent('mouseout', function(){
						$('image_help').getChildren().destroy();});
			$('help_orig').addEvent('mouseout', function(){
						$('image_help').getChildren().destroy();});
			$('help_sys').addEvent('mouseout', function(){
						$('image_help').getChildren().destroy();});					
			
				
			}
}); 

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

	function disappearow( obj ){
		theRow = $( obj ); 
		if(document.all){theRow.style.display = ( ( theRow.style.display == "block" ) ? "none" : "block" );}
		else theRow.style.display = ( ( theRow.style.display == "table-row" ) ? "none" : "table-row" );
	}


function pcCheck(el) {

    var pc = $('pc').value;

    // If the pc does not start with 28, 27 or 54, warn the user
    if ((pc.substr(0, 2) != '28' && pc.substr(0, 2) != '27' && pc.substr(0, 2) != '54') || pc.length != 8)
    {
        el.errors.push(formcheckLanguage['pcCheck']);
        return false;
    }
}

/* Call to a webservice to check if the product code is valid */
function pcLookUp() {
    //$('pc_valid').value = "true";
    var pc = $('pc').value;
	var modelname;
    var httpRequest = getHttpRequest();
    if (httpRequest && pc.length==8) {
        var url = '/shared/code/supportwebservs.asmx/getModelName?theProductCode=' + pc;
        httpRequest.open('GET', url, true);
        httpRequest.onreadystatechange = function() {
            if (httpRequest.readyState == 4) {
                if (httpRequest.status == 200) {
                    try {
                        modelname = httpRequest.responseXML.getElementsByTagName('string')[0].childNodes[0].nodeValue;
                        if (modelname != "0"){ $('pc').setStyle('background-color','#69da66'); $('pc').setStyle('color','#124c16'); }
                        else { $('pc').setStyle('background-color', '#bb362d'); $('pc').setStyle('color', '#fff'); }
                        $('mn').value = modelname.replace('/', '_');
                    }
                    catch (e) {
                        //alert(e);
                    }
                }
            }
        }
        httpRequest.send(null);
    }
    else {
        $('pc').setStyle('background-color', '#bb362d');
        $('pc').setStyle('color', '#fff');
        $('mn').value = "0";
    }
}

/*
function iClientSessionId(user) {
    var logininfo;
    if (user == "callc") logininfo = "username=id_rccallc&password=my_rccallc";
    else logininfo = "username=id_rccust&password=my_rccust";
    var httpRequest = getHttpRequest();
    if (httpRequest) {
        var url = 'http://stg.websrv.eu.sony.com/services/SessionService/login?' + logininfo;
        httpRequest.open('GET', url, true);
        httpRequest.onreadystatechange = function() {
            if (httpRequest.readyState == 4) {
                if (httpRequest.status == 200) {
                    try {
                        $('sessionId').value = httpRequest.responseXML.getElementsByTagName('ns:return')[0].childNodes[0].nodeValue;
                    }
                    catch (e) {
                        //alert(e);
                    }
                }
            }
        }
        httpRequest.send(null);
    }
}

function affectedTZCheck(productCode, serialNumber) {
    var httpRequest = getHttpRequest();
    if (httpRequest) {
        var url = 'http://stg.websrv.eu.sony.com/services/TZIssueService/getCompleteInfo?sessionId=?' + $('sessionId').value + "&productCode=" + productCode + "&serialNumber" + serialNumber;
        httpRequest.open('GET', url, true);
        httpRequest.onreadystatechange = function() {
            if (httpRequest.readyState == 4) {
                if (httpRequest.status == 200) {
                    try {
                        var root = httpRequest.responseXML.documentElement;
                        var ns_return = root.getElementsByTagName('ns:return')[0];
                        var ax22_endDate = ns_return.getElementsByTagName('ax22:endDate')[0];
                        $('affected').value = ax22_endDate.childNodes[0].nodeValue; 
                    }
                    catch (e) {
                        //alert(e);
                    }
                }
            }
        }
        httpRequest.send(null);
    }
}
*/

function pcFound() {
    var pc = $('pc').value;
    if ($('mn').value == "0" && ((pc.substr(0, 2) == '28' || pc.substr(0, 2) == '27' || pc.substr(0, 2) == '54') && pc.length == 8)) {
        alert(formcheckLanguage['pcNotFound']);
        return false;
    }
    else if ($('mn').value != "0") {
        if ($('dftype').checked) {
            Cookie.write('modelId',      $('mn').value, { duration: 365, path: "/" });
            Cookie.write('productCode', $('pc').value, { duration: 365, path: "/" });
            Cookie.write('serialNumber', $('sn').value, { duration: 365, path: "/" });
            writeSessionVariable('modelId', $('mn').value);
            writeSessionVariable('productCode', $('pc').value);
            writeSessionVariable('serialNumber', $('sn').value);
        }
		return true;
	}
}

function writeSessionVariable(variableName, variableValue) {
    var httpRequest = getHttpRequest();
    if (httpRequest) {
        var url = '/shared/code/supportwebservs.asmx/writeSessionVariable?variableName=' + variableName + '&variableValue=' + variableValue;
        httpRequest.open('GET', url, true);
        httpRequest.onreadystatechange = function() {
            if (httpRequest.readyState == 4) {
                if (httpRequest.status == 200) {
                    try {
                        text = httpRequest.responseXML.getElementsByTagName('boolean')[0].childNodes[0].nodeValue;
                    }
                    catch (e) {
                        //alert(e);
                    }
                }
            }
        }
        httpRequest.send(null);
    }
}

function modelSelected() {
    if ($('dfsel').checked) {
        Cookie.write('modelId', $('select_model').value, { duration: 365, path: "/" });
        Cookie.write('productCode', '', { duration: 365, path: "/" });
        Cookie.write('serialNumber', '', { duration: 365, path: "/" });
        writeSessionVariable('modelId', $('select_model').value);
        writeSessionVariable('productCode', '');
        writeSessionVariable('serialNumber', '');
    }
    $('dfsel').disabled = 'disabled';
    return true;
}

    
function vspInitTabNav(){
    try{
		
        var highest=0;
        var tabs=$$('.vsp-tab-nav-link span');
		
        tabs.each(function(el,i){
						   
			//tabs[i].setStyle('max-width',(680/tabs.length)+"px");	   
			tabs[i].setStyle('max-width', "250px");	 
            if(tabs[i].offsetHeight > highest){highest=tabs[i].offsetHeight}
				 // s: IE6 bug fix
            if(Browser.Engine.trident){
						if(tabs[i].offsetWidth > 150){
							tabs[i].setStyle('width',(680/tabs.length)-12+"px");}
							else{tabs[i].setStyle('width',"150px");};
						tabs[i].setStyle('padding-right',"2px");
						if(tabs[i].offsetHeight > highest){highest=tabs[i].offsetHeight}
            }
        });
        highest=highest-20;
        $$('.vsp-tab-nav-link span').each(function(el){
            $$('.vsp-tab-nav-link span').setStyle('height',highest);
			 // s: IE6 bug fix
            if(Browser.Engine.trident){
			            $$('.vsp-tab-nav-link span').setStyle('display','inline-block'); 
            }
            // e: IE6 bug fix
        });
    }catch(err){return false;}
	


}

function prefix_change() {
	prefix = $("prefix").value;
	phone_prefix = $("phone_prefix");

	if (prefix == "39") return_prefix = "0039";
	else if (prefix == "00" || prefix == "") return_prefix = "00";
	else return_prefix = "00" + prefix + "(0)";
	
	phone_prefix.value = return_prefix;
}

function country_change() {
    prefix = $("country").value.substring(3);
    phone_prefix = $("phone_prefix");

    if (prefix == "39") return_prefix = "0039";
    else if (prefix == "00" || prefix == "") return_prefix = "00";
    else return_prefix = "00" + prefix + "(0)";

    phone_prefix.value = return_prefix;
}

///\brief   hides the answers spans in the virs section */
function hidesections(){
	
		var source = $$('.questions');
		var target = $$('.answers');	
		
		source.each( function(linktoggler, i) {
			target[i].setStyle('display','none');
			linktoggler.onclick = function(){
			for(var j = 0; j < target.length; j++){
				
					target[j].setStyle('display','none');
				}
				show(target[i]);
				return false;
			
			}
		});		
	
	}

///\brief  
function show(target){
	theDiv = $(target);
	theDiv.style.display == "block" ? theDiv.style.display = "none" : theDiv.style.display = "block"
	}
	
function TZ_prefix_change() {
	prefix = $("prefix").value;
	mobilephone_prefix = $("mobilephone_prefix");
	officephone_prefix = $("officephone_prefix");
	if (prefix == "00" || prefix == "") return_prefix = "00";
	else if (prefix == "39") return_prefix = "00" + prefix;
	else return_prefix = "00" + prefix + "(0)";
	mobilephone_prefix.value = return_prefix;
	officephone_prefix.value = return_prefix;
}

function TZ_btn_cancel_click() {
	if (window.confirm(cancel_confirm)) window.history.back();
}

function TZ_nosurvey_check_click() {
    nosurvey_check = $("nosurvey_check");
    nosurvey = $("nosurvey");
    if (nosurvey_check.checked) nosurvey.value = "true";
    else nosurvey.value = "false";
}

function TZ_notsendadverts_check_click() {
    notsendadverts_check = $("notsendadverts_check");
    notsendadverts = $("notsendadverts");
    if (notsendadverts_check.checked) notsendadverts.value = "true";
    else notsendadverts.value = "false";
}

