function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
 if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

// Global object to hold drag information.

var dragObj = new Object();
dragObj.zIndex = 0;

function dragStart2(event, id) {

  var el;
  var x, y;

  // If an element id was given, find it. Otherwise use the element being
  // clicked on.

  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;

    // If this is a text node, use its parent element.

    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Save starting positions of cursor and element.

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top, 10);

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

  // Update element's z-index.

  dragObj.elNode.style.zIndex = ++dragObj.zIndex;

  // Capture mousemove and mouseup events on the page.

  if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo2);
    document.attachEvent("onmouseup",   dragStop2);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo2,   true);
    document.addEventListener("mouseup",   dragStop2, true);
    event.preventDefault();
  }
//alert(dragObj.elNode.style.top);
//alert(document.getElementById('card_self').offsetHeight);
    /*var left = ((dragObj.elStartLeft + event.clientX + window.scrollX)- dragObj.cursorStartX);*/
/*alert((dragObj.elStartLeft + x - dragObj.cursorStartX));
alert((dragObj.elStartLeft + x - dragObj.cursorStartX));*/
}

function dragGo2(event) {

  var x, y;

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }
  var drgobwidth=dragObj.elNode.style.width;
  drgobwidth=drgobwidth.split("px");
  drgobwidth=drgobwidth[0];
  if(!drgobwidth){
  /*drgobwidth=60;*/
  }

  

  
  var left = (dragObj.elStartLeft + x - dragObj.cursorStartX);
  var top  = (dragObj.elStartTop  + y - dragObj.cursorStartY);
   if (browser.isIE) {
   var max_left=((document.body.clientWidth)-(document.getElementById('tool_table').offsetWidth))-(120);
   
   }
   else{
   var max_left=((document.body.clientWidth)-(document.getElementById('tool_table').offsetWidth))-(80);
    
   }
   var max_top=document.getElementById('tool_table').offsetHeight;
   if(left >= max_left){
   dragObj.elNode.style.left =max_left + "px";
   var left_use=max_left;
   }
   else{
   dragObj.elNode.style.left =left + "px";
   var left_use=left;
   }
   if(top >= max_top){
   dragObj.elNode.style.top=max_top+"px";
   var top_use=max_top;
  }
  else{
    dragObj.elNode.style.top=top+"px";
      var top_use=top;
  }
     
   
     //====================changing the scratches width and height relatively===
      
    var actual_width_parent=document.getElementById('card_self').style.width;
var actual_height_parent=document.getElementById('card_self').offsetHeight;
actual_width_parent=actual_width_parent.split("px");
actual_width_parent=actual_width_parent[0];
    var percent=(left_use / actual_width_parent)*100;
    var percent_heigh=(top_use / actual_height_parent)*100;

   
    /*------change width and height of scratch img relatively-----*/
var scr_num=document.card_form.scr_num.value;
var width_arr=new Array();
var height_arr=new Array();
var arr_index=0;
for(var i=1;i<=scr_num;i++){
var img_id='img_scratch'+i;
var dif_width=0;
var dif_top=0;
var scratchwidth=document.getElementById(img_id).style.width;
scratchwidth=scratchwidth.split("px");
scratchwidth=scratchwidth[0];
scratchheight=document.getElementById(img_id).offsetHeight;
var scratch_newwidth=Math.round((scratchwidth*percent)/100);
var scratchleft=document.getElementById(img_id).style.left.split('px');
scratchleft=scratchleft[0];
//================arrays of width&height
width_arr[arr_index]=parseInt(10+parseInt(scratchwidth));
height_arr[arr_index]=parseInt(4+parseInt(scratchheight));
//========================================
var scratchtop=document.getElementById(img_id).style.top.split('px');
scratchtop=scratchtop[0];

   if(parseInt(left_use) < parseInt(actual_width_parent)){
   dif_width=parseInt(actual_width_parent)-parseInt(left_use);
   var scratch_newleft=parseInt(scratchleft)-parseInt(dif_width);
   }
   else{
   dif_width=parseInt(left_use) - parseInt(actual_width_parent);
   var scratch_newleft=parseInt(scratchleft)+parseInt(dif_width);
   }
   if(scratch_newleft <= 10){
   scratch_newleft=10;
   }
   
   //==================the top========
   var heigh_parent=(parseInt(actual_height_parent)-2);
   if((parseInt(top_use)) < parseInt(heigh_parent)){
   dif_top=parseInt(heigh_parent)-parseInt(top_use);
   var scratch_newtop=parseInt(scratchtop)-parseInt(dif_top);
   }
   else{
   dif_top=(parseInt(top_use))-parseInt(heigh_parent);
   var scratch_newtop=parseInt(scratchtop)+parseInt(dif_top);
   }
   if(scratch_newleft <= 10){
   scratch_newleft=10;
   }
   if(scratch_newtop <= 4){
   scratch_newtop=4;
   }
   //=========================

document.getElementById(img_id).style.top=(scratch_newtop)+"px";
document.getElementById(img_id).style.left=scratch_newleft+"px";
arr_index++;
}

/*----------width of the textdiv-----*/
for(var i=1;i<=document.card_form.txt_num.value;i++){
 var dif_top=0;
if(document.getElementById('txt_show'+i)){
var txtleft=document.getElementById('txt_show'+i).style.left.split('px');
txtleft=txtleft[0];
var txttop=document.getElementById('txt_show'+i).style.top.split('px');
txttop=txttop[0];
var textdivwidth=document.getElementById('txt_show'+i).offsetWidth;
var textdivheight=document.getElementById('txt_show'+i).offsetHeight;
var new_textwidth=Math.round((textdivwidth*percent)/100);
var new_textheight=Math.round((textdivheight*percent_heigh)/100);

width_arr[arr_index]=(parseInt(textdivwidth)+10);
height_arr[arr_index]=parseInt(4+parseInt(textdivheight));
//========================checking the left
    if(parseInt(left_use) < parseInt(actual_width_parent)){
    dif_width=parseInt(actual_width_parent)-parseInt(left_use);
    var txt_newleft=parseInt(txtleft)-parseInt(dif_width);
   }
   else{
   dif_width=parseInt(left_use) - parseInt(actual_width_parent);
   var txt_newleft=parseInt(txtleft)+parseInt(dif_width);
   }
   if(txt_newleft <= 10){
   txt_newleft=10;
   }
   //=======================checking the top===========
  var heigh_parent=(parseInt(actual_height_parent)-2);
   if((parseInt(top_use)) < parseInt(heigh_parent)){
   dif_top=parseInt(heigh_parent)-parseInt(top_use);
   var txt_newtop=parseInt(txttop)-parseInt(dif_top);
   }
   else{
   
   dif_top=(parseInt(top_use))-parseInt(heigh_parent);
    
   var txt_newtop=parseInt(txttop)+ parseInt(dif_top);
   }
 if(txt_newtop <= 4){
   txt_newtop=4;
   }
 
     //====================================
document.getElementById('txt_show'+i).style.left=txt_newleft+"px";
document.getElementById('txt_show'+i).style.top=txt_newtop+"px";
arr_index++;
}
}
document.getElementById('card_self').style.width=dragObj.elNode.style.left;
document.getElementById('card_self').style.height=dragObj.elNode.style.top;
document.getElementById('bg_img').style.width=dragObj.elNode.style.left;
document.getElementById('bg_img').style.height=dragObj.elNode.style.top;
var maxi=0;
for(var i=0;i<width_arr.length;i++){
if(maxi< width_arr[i]){
maxi=width_arr[i];
}
}

var maxh=0;
for(var i=0;i<height_arr.length;i++){
if(maxh< height_arr[i]){
maxh=height_arr[i];
}
}
if( left_use <= parseInt(maxi) ){
document.getElementById('card_self').style.width=maxi+'px';
dragObj.elNode.style.left=maxi+'px';
}
if( top_use <= parseInt(maxh) ){

document.getElementById('card_self').style.height=maxh+'px';
dragObj.elNode.style.top=maxh+'px';
}
//==============put the width and height in input====
var width_input=document.getElementById('card_self').style.width.split('px');
var height_input=document.getElementById('card_self').style.height.split('px');
document.card_form.width.value=width_input[0];
document.card_form.height.value=height_input[0];
//===================================================
/*----------------------------------------------------------------*/

   //====================================

  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}

function dragStop2(event) {

  // Stop capturing mousemove and mouseup events.

  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo2);
    document.detachEvent("onmouseup",   dragStop2);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo2,   true);
    document.removeEventListener("mouseup",   dragStop2, true);
  }
  var height_input=document.getElementById('card_self').style.height.split('px');
  if(parseInt(height_input[0])>=200){
document.getElementById('tool_table').style.height=parseInt(height_input[0])+'px';
}
}